├── .gitignore ├── Kbuild ├── LICENSE ├── Makefile ├── README.md ├── atapi-direct-irq-old-schematic.dts ├── atapi-direct-irq.dts ├── atapi-direct-old-schematic.dts ├── atapi-direct.dts ├── loaddt.sh ├── pata-gpio.c └── run.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | *.o 3 | *.o.* 4 | *.a 5 | *.s 6 | *.ko 7 | *.ko* 8 | *.so 9 | *.so.dbg 10 | *.mod.c 11 | *.i 12 | *.lst 13 | *.symtypes 14 | *.order 15 | modules.builtin 16 | *.elf 17 | *.bin 18 | *.gz 19 | *.bz2 20 | *.lzma 21 | *.xz 22 | *.lzo 23 | *.patch 24 | *.gcno 25 | !.gitignore 26 | *.symvers 27 | *.png 28 | *.jpg 29 | *.jpeg 30 | *.bmp 31 | *.dmp 32 | *.raw 33 | *.out 34 | *.gdb 35 | *.dtb 36 | *.dtbo 37 | *.dto 38 | *.testfiles 39 | *.mod 40 | -------------------------------------------------------------------------------- /Kbuild: -------------------------------------------------------------------------------- 1 | obj-m := pata-gpio.o 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | KDIR ?= /lib/modules/`uname -r`/build 2 | 3 | default: 4 | $(MAKE) -C $(KDIR) M=$$PWD 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pata-gpio 2 | ============================= 3 | 4 | ![Adapter PCB](https://screenshot.tbspace.de/nceyjhftwua.jpg) 5 | ![dmesg Output](https://screenshot.tbspace.de/iralxfbzwke.png) 6 | 7 | ### Overview 8 | 9 | **DISCLAIMER: This driver was developed for fun and learning ATA internals. If you're looking for a practical way to use PATA devices, look for commercial USB adapters.** 10 | 11 | pata-gpio is a Linux kernel driver for GPIO bitbanged PATA (also known as ATA/ATAPI or IDE). 12 | Pin configuration can be set via device tree, the regular ATA framework is used. 13 | 14 | IDE Primary/Secondary device support is implemented. 15 | 16 | ### Performance 17 | 18 | On a Raspberry Pi 4 (with direct GPIO) about **800 KiB/s reading, 500 KiB/s writing speed** is possible. 19 | Most of the performance bottleneck is down to the usage of libgpiod (kernel gpio abstraction), 20 | which is optimized for tasks like switching LEDs. Data transmission is well outside the design scope of libgpiod. 21 | 22 | DMA and interrupts are not implemented at the moment. 23 | 24 | ### Device tree configuration 25 | 26 | Pin configuration can be specified in the device tree. 27 | An example device tree is provided as [atapi-direct.dts](https://github.com/Manawyrm/pata-gpio/blob/master/atapi-direct.dts). 28 | This includes the pin configuration for the schematic linked at the bottom of this readme. 29 | 30 | The following device tree properties are present: 31 | 32 | | Property | Usage | Required | 33 | | ------------- |-------------| -----| 34 | | `databus-gpios` | list of 16 GPIO pins, connected to ATA D0 - D15 in this order | yes | 35 | | `cs-gpios` | list of 2 GPIO pins, connected to ATA CS0 and CS1 in this order | yes | 36 | | `address-gpios` | list of 3 GPIO pins, connected to ATA DA0, DA1 and DA2 in this order | yes | 37 | | `strobe-write-gpio` | GPIO pin, connected to ATA IOW line | yes | 38 | | `strobe-read-gpio` | GPIO pin, connected to ATA IOR line | yes | 39 | | `reset-gpio` | GPIO pin, connected to ATA RESET line | no | 40 | 41 | #### Interrupts 42 | 43 | The driver has experimental interrupt support. An example configuration with GPIO interrupts is outlined in 44 | `atapi-direct-irq.dts`. 45 | Interrupts are configured via the generic `interrupts` property. If the `interrupts` property is not specified 46 | the driver falls back to polling. 47 | 48 | ### Compiling 49 | 50 | Before loading (and running) the kernel module, make sure to load the device tree overlay. 51 | This can be done via the config.txt (on a Raspberry Pi): `dtoverlay=atapi-direct`, or by running `./loaddt.sh`. 52 | The latter script will also compile and save the device tree to `/boot/overlays/atapi-direct.dtbo`. 53 | 54 | This module needs kernel headers, regular build dependencies and a kernel with the config option `CONFIG_ATA_SFF=y` set. 55 | The `ATA_SFF` option is not the default on Raspberry Pis, so you will need to build a custom kernel. 56 | 57 | Compile the module using: 58 | `make` 59 | 60 | Load the module using: 61 | `insmod pata-gpio.ko` 62 | 63 | A helper script called `./run.sh` is provided, which will remove any loaded pata-gpio module, compile it and load the resulting module. 64 | Further status information can be shown by running `dmesg`. 65 | 66 | ### Additional info 67 | 68 | [Adapter PCB](https://github.com/Manawyrm/ATAPIHat-SMI) 69 | [Adapter schematic](https://github.com/Manawyrm/ATAPIHat-SMI/blob/master/gerbers/RevA/ATAPIHat-SMI/ATAPIHat-SMI.pdf) 70 | 71 | [Older schematic (not SMI-ready)](https://screenshot.tbspace.de/cmzhjfglbya.png) 72 | [Video: Audio CD streaming](https://www.youtube.com/watch?v=cHQhuzSn2oE) 73 | [Twitter: Project presentation](https://twitter.com/Manawyrm/status/1292084926980984833) 74 | 75 | ### Credits 76 | Thanks to [@Toble_Miner](https://github.com/TobleMiner) for a lot of help in the development of this driver! 77 | -------------------------------------------------------------------------------- /atapi-direct-irq-old-schematic.dts: -------------------------------------------------------------------------------- 1 | /dts-v1/; 2 | /plugin/; 3 | 4 | / { 5 | compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709", "brcm,bcm2911"; 6 | 7 | fragment@0 { 8 | target = <&gpio>; 9 | 10 | __overlay__ { 11 | pata_irq_pin: pata-irq-pin { 12 | brcm,pins = <26>; 13 | brcm,function = <0>; /* GPIO, input */ 14 | brcm,pull = <1>; 15 | }; 16 | }; 17 | }; 18 | 19 | fragment@1 { 20 | target-path = "/"; 21 | __overlay__ { 22 | pata-gpio { 23 | compatible = "pata-gpio"; 24 | 25 | address-gpios = < 26 | &gpio 13 0 27 | &gpio 6 0 28 | &gpio 1 0 29 | >; 30 | 31 | cs-gpios = < 32 | &gpio 19 1 33 | &gpio 12 1 34 | >; 35 | 36 | databus-gpios = < 37 | &gpio 11 0 38 | &gpio 9 0 39 | &gpio 10 0 40 | &gpio 22 0 41 | &gpio 27 0 42 | &gpio 17 0 43 | &gpio 4 0 44 | &gpio 3 0 45 | &gpio 16 0 46 | &gpio 20 0 47 | &gpio 18 0 48 | &gpio 23 0 49 | &gpio 24 0 50 | &gpio 25 0 51 | &gpio 8 0 52 | &gpio 7 0 53 | >; 54 | 55 | interrupts = <26 0x01>; /* GPIO26, rising edge */ 56 | interrupt-parent = <&gpio>; 57 | 58 | pinctrl-default = <&pata_irq_pin>; 59 | pinctrl-names = "default"; 60 | 61 | reset-gpio = <&gpio 2 1>; 62 | 63 | strobe-read-gpio = <&gpio 5 1>; 64 | strobe-write-gpio = <&gpio 0 1>; 65 | 66 | status = "okay"; 67 | }; 68 | }; 69 | }; 70 | }; 71 | -------------------------------------------------------------------------------- /atapi-direct-irq.dts: -------------------------------------------------------------------------------- 1 | /dts-v1/; 2 | /plugin/; 3 | 4 | / { 5 | compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709", "brcm,bcm2911"; 6 | 7 | fragment@0 { 8 | target = <&gpio>; 9 | 10 | __overlay__ { 11 | pata_irq_pin: pata-irq-pin { 12 | brcm,pins = <27>; 13 | brcm,function = <0>; /* GPIO, input */ 14 | brcm,pull = <1>; 15 | }; 16 | }; 17 | }; 18 | 19 | fragment@1 { 20 | target-path = "/"; 21 | __overlay__ { 22 | pata-gpio { 23 | compatible = "pata-gpio"; 24 | 25 | address-gpios = < 26 | &gpio 5 0 27 | &gpio 4 0 28 | &gpio 3 0 29 | >; 30 | 31 | cs-gpios = < 32 | &gpio 2 1 33 | &gpio 1 1 34 | >; 35 | 36 | databus-gpios = < 37 | &gpio 8 0 38 | &gpio 9 0 39 | &gpio 10 0 40 | &gpio 11 0 41 | &gpio 12 0 42 | &gpio 13 0 43 | &gpio 14 0 44 | &gpio 15 0 45 | &gpio 16 0 46 | &gpio 17 0 47 | &gpio 18 0 48 | &gpio 19 0 49 | &gpio 20 0 50 | &gpio 21 0 51 | &gpio 22 0 52 | &gpio 23 0 53 | >; 54 | 55 | interrupts = <27 0x01>; /* GPIO27, rising edge */ 56 | interrupt-parent = <&gpio>; 57 | 58 | pinctrl-default = <&pata_irq_pin>; 59 | pinctrl-names = "default"; 60 | 61 | reset-gpio = <&gpio 26 1>; 62 | 63 | strobe-read-gpio = <&gpio 6 1>; 64 | strobe-write-gpio = <&gpio 7 1>; 65 | 66 | status = "okay"; 67 | }; 68 | }; 69 | }; 70 | }; 71 | -------------------------------------------------------------------------------- /atapi-direct-old-schematic.dts: -------------------------------------------------------------------------------- 1 | /dts-v1/; 2 | /plugin/; 3 | 4 | / { 5 | compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709", "brcm,bcm2911"; 6 | 7 | fragment@0 { 8 | target-path = "/"; 9 | __overlay__ { 10 | pata-gpio { 11 | compatible = "pata-gpio"; 12 | databus-gpios = < 13 | &gpio 11 0 14 | &gpio 9 0 15 | &gpio 10 0 16 | &gpio 22 0 17 | &gpio 27 0 18 | &gpio 17 0 19 | &gpio 4 0 20 | &gpio 3 0 21 | &gpio 14 0 22 | &gpio 15 0 23 | &gpio 18 0 24 | &gpio 23 0 25 | &gpio 24 0 26 | &gpio 25 0 27 | &gpio 8 0 28 | &gpio 7 0 29 | >; 30 | 31 | reset-gpio = <&gpio 2 1>; 32 | 33 | cs-gpios = < 34 | &gpio 19 1 35 | &gpio 12 1 36 | >; 37 | 38 | address-gpios = < 39 | &gpio 13 0 40 | &gpio 6 0 41 | &gpio 1 0 42 | >; 43 | 44 | strobe-write-gpio = <&gpio 0 1>; 45 | strobe-read-gpio = <&gpio 5 1>; 46 | 47 | status = "okay"; 48 | }; 49 | }; 50 | }; 51 | }; 52 | -------------------------------------------------------------------------------- /atapi-direct.dts: -------------------------------------------------------------------------------- 1 | /dts-v1/; 2 | /plugin/; 3 | 4 | / { 5 | compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709", "brcm,bcm2911"; 6 | 7 | fragment@0 { 8 | target-path = "/"; 9 | __overlay__ { 10 | pata-gpio { 11 | compatible = "pata-gpio"; 12 | 13 | address-gpios = < 14 | &gpio 5 0 15 | &gpio 4 0 16 | &gpio 3 0 17 | >; 18 | 19 | cs-gpios = < 20 | &gpio 2 1 21 | &gpio 1 1 22 | >; 23 | 24 | databus-gpios = < 25 | &gpio 8 0 26 | &gpio 9 0 27 | &gpio 10 0 28 | &gpio 11 0 29 | &gpio 12 0 30 | &gpio 13 0 31 | &gpio 14 0 32 | &gpio 15 0 33 | &gpio 16 0 34 | &gpio 17 0 35 | &gpio 18 0 36 | &gpio 19 0 37 | &gpio 20 0 38 | &gpio 21 0 39 | &gpio 22 0 40 | &gpio 23 0 41 | >; 42 | 43 | reset-gpio = <&gpio 26 1>; 44 | 45 | strobe-read-gpio = <&gpio 6 1>; 46 | strobe-write-gpio = <&gpio 7 1>; 47 | 48 | status = "okay"; 49 | }; 50 | }; 51 | }; 52 | }; 53 | -------------------------------------------------------------------------------- /loaddt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | dtoverlay -R atapi-direct 4 | dtc -I dts -O dtb atapi-direct.dts > /boot/overlays/atapi-direct.dtbo 5 | dtoverlay /boot/overlays/atapi-direct.dtbo 6 | -------------------------------------------------------------------------------- /pata-gpio.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // CS0 High / CS1 Low 14 | #define REG_DATA 0x00 15 | #define REG_ERROR 0x01 16 | #define REG_FEATURE 0x01 17 | #define REG_NSECT 0x02 18 | #define REG_LBAL 0x03 19 | #define REG_LBAM 0x04 20 | #define REG_LBAH 0x05 21 | #define REG_DEVICE 0x06 22 | #define REG_STATUS 0x07 23 | #define REG_COMMAND 0x07 24 | 25 | // CS1 High / CS0 Low 26 | #define REG_ALTSTATUS 0x10 27 | #define REG_CTL 0x10 28 | 29 | // Invalid register cache value 30 | #define REG_INVALID 0xff 31 | 32 | #if 0 33 | #define pata_ndelay(x) ndelay(x) 34 | #else 35 | #define pata_ndelay(x) 36 | #endif 37 | 38 | struct pata_gpio { 39 | struct device *dev; 40 | struct gpio_descs *databus_gpios; 41 | struct gpio_desc *reset_gpio; 42 | struct gpio_descs *cs_gpios; 43 | struct gpio_descs *address_gpios; 44 | struct gpio_desc *strobe_write_gpio; 45 | struct gpio_desc *strobe_read_gpio; 46 | u8 last_reg; 47 | const struct ata_timing *timing; 48 | }; 49 | 50 | static int pata_gpio_set_register(struct pata_gpio *pata, unsigned long reg) 51 | { 52 | int err; 53 | unsigned long cs_state = 0b01; 54 | 55 | if (pata->last_reg == reg) { 56 | return 0; 57 | } 58 | 59 | if (reg & 0xF0) 60 | cs_state = 0b10; 61 | 62 | err = gpiod_set_array_value(pata->cs_gpios->ndescs, 63 | pata->cs_gpios->desc, 64 | pata->cs_gpios->info, 65 | &cs_state); 66 | if (err) 67 | return err; 68 | 69 | err = gpiod_set_array_value(pata->address_gpios->ndescs, 70 | pata->address_gpios->desc, 71 | pata->address_gpios->info, 72 | ®); 73 | if (err) 74 | return err; 75 | 76 | pata->last_reg = reg; 77 | 78 | return 0; 79 | } 80 | 81 | static int __pata_gpio_read16_no_iocfg(struct pata_gpio *pata, u8 reg, u16 *result) { 82 | int err; 83 | unsigned long value = 0; 84 | 85 | err = pata_gpio_set_register(pata, reg); 86 | if (err) 87 | return err; 88 | if (pata->timing) 89 | pata_ndelay(pata->timing->setup); 90 | 91 | gpiod_set_value(pata->strobe_read_gpio, 1); 92 | if (pata->timing) 93 | pata_ndelay(pata->timing->act8b); 94 | 95 | err = gpiod_get_array_value(pata->databus_gpios->ndescs, 96 | pata->databus_gpios->desc, 97 | pata->databus_gpios->info, 98 | &value); 99 | 100 | gpiod_set_value(pata->strobe_read_gpio, 0); 101 | 102 | if (!err) 103 | *result = value; 104 | 105 | return err; 106 | } 107 | 108 | static int pata_gpio_read16(struct pata_gpio *pata, u8 reg, u16 *result) 109 | { 110 | u8 i; 111 | int err; 112 | 113 | for (i = 0; i < pata->databus_gpios->ndescs; i++) { 114 | err = gpiod_direction_input(pata->databus_gpios->desc[i]); 115 | if (err) 116 | return err; 117 | } 118 | 119 | return __pata_gpio_read16_no_iocfg(pata, reg, result); 120 | } 121 | 122 | static int __pata_gpio_write16_no_iocfg(struct pata_gpio *pata, unsigned long value) 123 | { 124 | int err; 125 | 126 | err = gpiod_set_array_value(pata->databus_gpios->ndescs, pata->databus_gpios->desc, pata->databus_gpios->info, &value); 127 | if (err) 128 | return err; 129 | if (pata->timing) 130 | pata_ndelay(pata->timing->setup); 131 | 132 | gpiod_set_value(pata->strobe_write_gpio, 1); 133 | if (pata->timing) 134 | pata_ndelay(pata->timing->act8b); 135 | gpiod_set_value(pata->strobe_write_gpio, 0); 136 | if (pata->timing) 137 | pata_ndelay(pata->timing->rec8b); 138 | 139 | return 0; 140 | } 141 | 142 | static int pata_gpio_write16(struct pata_gpio *pata, u8 reg, unsigned long value) 143 | { 144 | u8 i; 145 | int err; 146 | 147 | err = pata_gpio_set_register(pata, reg); 148 | if (err) 149 | return err; 150 | if (pata->timing) 151 | pata_ndelay(pata->timing->setup); 152 | 153 | for (i = 0; i < pata->databus_gpios->ndescs; i++) { 154 | err = gpiod_direction_output(pata->databus_gpios->desc[i], (value >> i) & 0x01); 155 | if (err) 156 | return err; 157 | } 158 | 159 | gpiod_set_value(pata->strobe_write_gpio, 1); 160 | if (pata->timing) 161 | pata_ndelay(pata->timing->act8b); 162 | gpiod_set_value(pata->strobe_write_gpio, 0); 163 | if (pata->timing) 164 | pata_ndelay(pata->timing->rec8b); 165 | 166 | for (i = 0; i < pata->databus_gpios->ndescs; i++) { 167 | err = gpiod_direction_input(pata->databus_gpios->desc[i]); 168 | if (err) 169 | return err; 170 | } 171 | 172 | return 0; 173 | } 174 | 175 | static void pata_gpio_write16_safe(struct pata_gpio *pata, u8 reg, unsigned long value) 176 | { 177 | int err; 178 | 179 | err = pata_gpio_write16(pata, reg, value); 180 | if (err) { 181 | dev_err(pata->dev, "failed to write gpios in %s, code %d\n", __func__, err); 182 | BUG(); 183 | } 184 | } 185 | 186 | static u16 pata_gpio_read16_safe(struct pata_gpio *pata, u8 reg) 187 | { 188 | u16 result; 189 | int err; 190 | 191 | err = pata_gpio_read16(pata, reg, &result); 192 | if (err) { 193 | dev_err(pata->dev, "failed to read gpios in %s, code %d\n", __func__, err); 194 | BUG(); 195 | } 196 | 197 | return result; 198 | } 199 | 200 | /* 201 | * pata_gpio_check_status - Read device status register 202 | */ 203 | static u8 pata_gpio_check_status(struct ata_port *ap) 204 | { 205 | return pata_gpio_read16_safe(ap->host->private_data, REG_STATUS) & 0xFF; 206 | } 207 | 208 | /* 209 | * pata_gpio_check_altstatus - Read alternate device status register 210 | */ 211 | static u8 pata_gpio_check_altstatus(struct ata_port *ap) 212 | { 213 | return pata_gpio_read16_safe(ap->host->private_data, REG_ALTSTATUS) & 0xFF; 214 | } 215 | 216 | /* 217 | * pata_gpio_exec_command - issue ATA command to host controller 218 | */ 219 | static void pata_gpio_exec_command(struct ata_port *ap, 220 | const struct ata_taskfile *tf) 221 | { 222 | pata_gpio_write16_safe(ap->host->private_data, REG_COMMAND, tf->command); 223 | ata_sff_pause(ap); 224 | } 225 | 226 | /* 227 | * pata_gpio_tf_load - send taskfile registers to host controller 228 | */ 229 | static void pata_gpio_tf_load(struct ata_port *ap, 230 | const struct ata_taskfile *tf) 231 | { 232 | struct pata_gpio *pata = ap->host->private_data; 233 | unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR; 234 | 235 | if (tf->ctl != ap->last_ctl) { 236 | pata_gpio_write16_safe(ap->host->private_data, REG_CTL, tf->ctl); 237 | ap->last_ctl = tf->ctl; 238 | ata_wait_idle(ap); 239 | } 240 | 241 | if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) { 242 | pata_gpio_write16_safe(pata, REG_FEATURE, tf->hob_feature); 243 | pata_gpio_write16_safe(pata, REG_NSECT, tf->hob_nsect); 244 | pata_gpio_write16_safe(pata, REG_LBAL, tf->hob_lbal); 245 | pata_gpio_write16_safe(pata, REG_LBAM, tf->hob_lbam); 246 | pata_gpio_write16_safe(pata, REG_LBAH, tf->hob_lbah); 247 | } 248 | 249 | if (is_addr) { 250 | pata_gpio_write16_safe(pata, REG_FEATURE, tf->feature); 251 | pata_gpio_write16_safe(pata, REG_NSECT, tf->nsect); 252 | pata_gpio_write16_safe(pata, REG_LBAL, tf->lbal); 253 | pata_gpio_write16_safe(pata, REG_LBAM, tf->lbam); 254 | pata_gpio_write16_safe(pata, REG_LBAH, tf->lbah); 255 | } 256 | 257 | if (tf->flags & ATA_TFLAG_DEVICE) 258 | pata_gpio_write16_safe(pata, REG_DEVICE, tf->device); 259 | 260 | ata_wait_idle(ap); 261 | } 262 | 263 | /* 264 | * pata_gpio_tf_read - input device's ATA taskfile shadow registers 265 | */ 266 | static void pata_gpio_tf_read(struct ata_port *ap, struct ata_taskfile *tf) 267 | { 268 | struct pata_gpio *pata = ap->host->private_data; 269 | 270 | tf->feature = pata_gpio_read16_safe(pata, REG_FEATURE); 271 | tf->nsect = pata_gpio_read16_safe(pata, REG_NSECT); 272 | tf->lbal = pata_gpio_read16_safe(pata, REG_LBAL); 273 | tf->lbam = pata_gpio_read16_safe(pata, REG_LBAM); 274 | tf->lbah = pata_gpio_read16_safe(pata, REG_LBAH); 275 | tf->device = pata_gpio_read16_safe(pata, REG_DEVICE); 276 | 277 | if (tf->flags & ATA_TFLAG_LBA48) { 278 | pata_gpio_write16_safe(pata, REG_CTL, tf->ctl | ATA_HOB); 279 | 280 | tf->hob_feature = pata_gpio_read16_safe(pata, REG_FEATURE); 281 | tf->hob_nsect = pata_gpio_read16_safe(pata, REG_NSECT); 282 | tf->hob_lbal = pata_gpio_read16_safe(pata, REG_LBAL); 283 | tf->hob_lbam = pata_gpio_read16_safe(pata, REG_LBAM); 284 | tf->hob_lbah = pata_gpio_read16_safe(pata, REG_LBAH); 285 | 286 | pata_gpio_write16_safe(pata, REG_CTL, tf->ctl); 287 | ap->last_ctl = tf->ctl; 288 | } 289 | } 290 | 291 | /* 292 | * pata_gpio_data_xfer - Transfer data by PIO 293 | */ 294 | static unsigned int pata_gpio_data_xfer(struct ata_queued_cmd *qc, 295 | unsigned char *buf, unsigned int buflen, int rw) 296 | { 297 | unsigned int i; 298 | unsigned int words = buflen >> 1; 299 | struct ata_port *ap = qc->dev->link->ap; 300 | struct pata_gpio *pata = ap->host->private_data; 301 | u16 *buf16 = (u16 *) buf; 302 | int err; 303 | 304 | /* Transfer multiple of 2 bytes */ 305 | if (rw == READ) { 306 | buf16[0] = pata_gpio_read16_safe(pata, REG_DATA); 307 | for (i = 1; i < words; i++) { 308 | err = __pata_gpio_read16_no_iocfg(pata, REG_DATA, &buf16[i]); 309 | if (err) { 310 | dev_err(ap->dev, "failed to read gpios in %s, code %d\n", __func__, err); 311 | BUG(); 312 | } 313 | } 314 | } else { 315 | err = pata_gpio_set_register(pata, REG_DATA); 316 | if (err) 317 | return err; 318 | if (pata->timing) 319 | pata_ndelay(pata->timing->setup); 320 | 321 | for (i = 0; i < pata->databus_gpios->ndescs; i++) { 322 | err = gpiod_direction_output(pata->databus_gpios->desc[i], 0); 323 | if (err) 324 | return err; 325 | } 326 | 327 | for (i = 0; i < words; i++) { 328 | err = __pata_gpio_write16_no_iocfg(pata, buf16[i]); 329 | if (err) 330 | return err; 331 | } 332 | 333 | for (i = 0; i < pata->databus_gpios->ndescs; i++) { 334 | err = gpiod_direction_input(pata->databus_gpios->desc[i]); 335 | if (err) 336 | return err; 337 | } 338 | } 339 | 340 | /* Transfer trailing 1 byte, if any. */ 341 | if (unlikely(buflen & 0x01)) { 342 | unsigned char *trailing_buf = buf + buflen - 1; 343 | 344 | dev_warn(ap->dev, "pata_gpio_data_xfer did uneven length xfer!\n"); 345 | 346 | if (rw == READ) { 347 | *trailing_buf = pata_gpio_read16_safe(pata, REG_DATA) & 0xFF; 348 | } else { 349 | pata_gpio_write16_safe(pata, REG_DATA, *trailing_buf); 350 | } 351 | } 352 | 353 | return buflen; 354 | } 355 | 356 | /* 357 | * pata_gpio_set_devctl - Write device control register 358 | */ 359 | static void pata_gpio_set_devctl(struct ata_port *ap, u8 ctl) 360 | { 361 | pata_gpio_write16_safe(ap->host->private_data, REG_CTL, ctl); 362 | } 363 | 364 | /* 365 | * pata_gpio_dev_select - Select device on ATA bus 366 | */ 367 | static void pata_gpio_dev_select(struct ata_port *ap, unsigned int device) 368 | { 369 | u8 tmp = ATA_DEVICE_OBS; 370 | 371 | if (device != 0) 372 | tmp |= ATA_DEV1; 373 | 374 | pata_gpio_write16_safe(ap->host->private_data, REG_DEVICE, tmp); 375 | ata_sff_pause(ap); 376 | } 377 | 378 | /* 379 | * pata_gpio_devchk - PATA device presence detection 380 | */ 381 | static bool pata_gpio_devchk(struct ata_port *ap, 382 | unsigned int device) 383 | { 384 | struct pata_gpio *pata = ap->host->private_data; 385 | u8 nsect, lbal; 386 | 387 | pata_gpio_dev_select(ap, device); 388 | 389 | pata_gpio_write16_safe(pata, REG_NSECT, 0x55); 390 | pata_gpio_write16_safe(pata, REG_LBAL, 0xAA); 391 | 392 | pata_gpio_write16_safe(pata, REG_NSECT, 0xAA); 393 | pata_gpio_write16_safe(pata, REG_LBAL, 0x55); 394 | 395 | pata_gpio_write16_safe(pata, REG_NSECT, 0x55); 396 | pata_gpio_write16_safe(pata, REG_LBAL, 0xAA); 397 | 398 | nsect = pata_gpio_read16_safe(pata, REG_NSECT); 399 | lbal = pata_gpio_read16_safe(pata, REG_LBAL); 400 | 401 | return ((nsect == 0x55) && (lbal == 0xaa)); 402 | } 403 | 404 | /* 405 | * pata_gpio_wait_after_reset - wait for devices to become ready after reset 406 | */ 407 | static int pata_gpio_wait_after_reset(struct ata_link *link, 408 | unsigned long deadline) 409 | { 410 | int rc; 411 | 412 | ata_msleep(link->ap, ATA_WAIT_AFTER_RESET); 413 | 414 | /* always check readiness of the master device */ 415 | rc = ata_sff_wait_ready(link, deadline); 416 | /* -ENODEV means the odd clown forgot the D7 pulldown resistor 417 | * and TF status is 0xff, bail out on it too. 418 | */ 419 | if (rc) 420 | return rc; 421 | 422 | return 0; 423 | } 424 | 425 | /* 426 | * pata_gpio_bus_softreset - PATA device software reset 427 | */ 428 | static int pata_gpio_bus_softreset(struct ata_port *ap, 429 | unsigned long deadline) 430 | { 431 | struct pata_gpio *pata = ap->host->private_data; 432 | 433 | /* software reset. causes dev0 to be selected */ 434 | pata_gpio_write16_safe(pata, REG_CTL, ap->ctl); 435 | udelay(20); 436 | pata_gpio_write16_safe(pata, REG_CTL, ap->ctl | ATA_SRST); 437 | udelay(20); 438 | pata_gpio_write16_safe(pata, REG_CTL, ap->ctl); 439 | ap->last_ctl = ap->ctl; 440 | 441 | return pata_gpio_wait_after_reset(&ap->link, deadline); 442 | } 443 | 444 | /* 445 | * pata_gpio_softreset - reset host port via ATA SRST 446 | */ 447 | static int pata_gpio_softreset(struct ata_link *link, unsigned int *classes, 448 | unsigned long deadline) 449 | { 450 | struct ata_port *ap = link->ap; 451 | int rc; 452 | u8 err; 453 | 454 | /* issue bus reset */ 455 | rc = pata_gpio_bus_softreset(ap, deadline); 456 | if (rc && rc != -ENODEV && rc != -EBUSY) { 457 | ata_link_err(link, "SRST failed (errno=%d)\n", rc); 458 | return rc; 459 | } 460 | 461 | /* determine by signature whether we have ATA or ATAPI devices */ 462 | classes[0] = ata_sff_dev_classify(&ap->link.device[0], pata_gpio_devchk(ap, 0), &err); 463 | classes[1] = ata_sff_dev_classify(&ap->link.device[1], pata_gpio_devchk(ap, 1), &err); 464 | 465 | return 0; 466 | } 467 | 468 | static void pata_gpio_drain_fifo(struct ata_queued_cmd *qc) 469 | { 470 | int count; 471 | struct ata_port *ap; 472 | struct pata_gpio *pata; 473 | u16 scratch; 474 | 475 | /* We only need to flush incoming data when a command was running */ 476 | if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE) 477 | return; 478 | 479 | ap = qc->ap; 480 | pata = ap->host->private_data; 481 | /* Drain up to 64K of data before we give up this recovery method */ 482 | if (ap->ops->sff_check_status(ap) & ATA_DRQ) { 483 | pata_gpio_read16_safe(pata, REG_DATA); 484 | for (count = 2; (ap->ops->sff_check_status(ap) & ATA_DRQ) && count < 65536; count += 2) 485 | __pata_gpio_read16_no_iocfg(pata, REG_DATA, &scratch); 486 | } 487 | } 488 | 489 | void pata_gpio_set_piomode(struct ata_port *ap, struct ata_device *dev) { 490 | struct pata_gpio *pata = ap->host->private_data; 491 | 492 | pata->timing = ata_timing_find_mode(dev->pio_mode); 493 | } 494 | 495 | static struct scsi_host_template pata_gpio_sht = { 496 | ATA_PIO_SHT("pata-gpio"), 497 | }; 498 | 499 | static struct ata_port_operations pata_gpio_port_ops = { 500 | .inherits = &ata_sff_port_ops, 501 | .set_piomode = pata_gpio_set_piomode, 502 | .sff_check_status = pata_gpio_check_status, 503 | .sff_check_altstatus = pata_gpio_check_altstatus, 504 | .sff_tf_load = pata_gpio_tf_load, 505 | .sff_tf_read = pata_gpio_tf_read, 506 | .sff_data_xfer = pata_gpio_data_xfer, 507 | .sff_drain_fifo = pata_gpio_drain_fifo, 508 | .sff_exec_command = pata_gpio_exec_command, 509 | .sff_dev_select = pata_gpio_dev_select, 510 | .sff_set_devctl = pata_gpio_set_devctl, 511 | .softreset = pata_gpio_softreset, 512 | }; 513 | 514 | static int claim_gpios(struct gpio_descs **target, unsigned count, const char *name, enum gpiod_flags flags, struct device *dev) 515 | { 516 | struct gpio_descs *gpios; 517 | 518 | gpios = devm_gpiod_get_array(dev, name, flags); 519 | if (!gpios) { 520 | return -ENOMEM; 521 | } 522 | if (gpios->ndescs != count) { 523 | return -EINVAL; 524 | } 525 | 526 | *target = gpios; 527 | return 0; 528 | } 529 | 530 | static int pata_gpio_probe(struct platform_device *pdev) 531 | { 532 | int err, irq; 533 | struct device *dev = &pdev->dev; 534 | struct pata_gpio *pata; 535 | struct ata_host *host; 536 | struct ata_port *ap; 537 | 538 | pata = devm_kzalloc(dev, sizeof(struct pata_gpio), GFP_KERNEL); 539 | if (!pata) 540 | return -ENOMEM; 541 | 542 | pata->dev = dev; 543 | pata->last_reg = REG_INVALID; 544 | 545 | err = claim_gpios(&pata->databus_gpios, 16, "databus", GPIOD_IN, dev); 546 | if (err) { 547 | dev_err(dev, "Failed to request databus gpios: %d\n", err); 548 | return err; 549 | } 550 | 551 | err = claim_gpios(&pata->cs_gpios, 2, "cs", GPIOD_OUT_LOW, dev); 552 | if (err) { 553 | dev_err(dev, "Failed to request cs gpios: %d\n", err); 554 | return err; 555 | } 556 | 557 | err = claim_gpios(&pata->address_gpios, 3, "address", GPIOD_OUT_LOW, dev); 558 | if (err) { 559 | dev_err(dev, "Failed to request address gpios: %d\n", err); 560 | return err; 561 | } 562 | 563 | pata->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 564 | 565 | if (IS_ERR(pata->reset_gpio)) 566 | return PTR_ERR(pata->reset_gpio); 567 | 568 | pata->strobe_read_gpio = devm_gpiod_get(dev, "strobe-read", GPIOD_OUT_LOW); 569 | if (!pata->strobe_read_gpio) 570 | return -ENOMEM; 571 | 572 | if (IS_ERR(pata->strobe_read_gpio)) 573 | return PTR_ERR(pata->strobe_read_gpio); 574 | 575 | pata->strobe_write_gpio = devm_gpiod_get(dev, "strobe-write", GPIOD_OUT_LOW); 576 | if (!pata->strobe_write_gpio) 577 | return -ENOMEM; 578 | 579 | if (IS_ERR(pata->strobe_write_gpio)) 580 | return PTR_ERR(pata->strobe_write_gpio); 581 | 582 | // hard-reset 583 | if (pata->reset_gpio) { 584 | gpiod_set_value(pata->reset_gpio, 1); 585 | udelay(20); 586 | gpiod_set_value(pata->reset_gpio, 0); 587 | msleep(100); 588 | } 589 | 590 | irq = platform_get_irq_optional(pdev, 0); 591 | if (!irq || irq == -ENXIO) { 592 | dev_warn(dev, "No irq configured, continuing without irq support\n"); 593 | irq = 0; 594 | } else if (irq < 0) { 595 | if (irq != -EPROBE_DEFER) 596 | dev_err(dev, "Failed to request irq: %d\n", irq); 597 | return irq; 598 | } 599 | 600 | host = ata_host_alloc(&pdev->dev, 1); 601 | if (!host) { 602 | dev_err(dev, "failed to allocate ide host\n"); 603 | return -ENOMEM; 604 | } 605 | 606 | host->private_data = pata; 607 | 608 | ap = host->ports[0]; 609 | ap->ops = &pata_gpio_port_ops; 610 | ap->pio_mask = ATA_PIO2; // Limited to PIO2 due to lack of IORDY support 611 | ap->flags = ATA_FLAG_SLAVE_POSS; 612 | if (!irq) 613 | ap->flags |= ATA_FLAG_PIO_POLLING; 614 | 615 | return ata_host_activate(host, irq, irq ? ata_sff_interrupt : NULL, 0, &pata_gpio_sht); 616 | } 617 | 618 | static const struct of_device_id pata_gpio_dt_ids[] = { 619 | { .compatible = "pata-gpio" }, 620 | {} 621 | }; 622 | MODULE_DEVICE_TABLE(of, pata_gpio_dt_ids); 623 | 624 | 625 | static struct platform_driver pata_gpio_driver = { 626 | .driver = { 627 | .name = "pata-gpio", 628 | .of_match_table = of_match_ptr(pata_gpio_dt_ids), 629 | }, 630 | .probe = pata_gpio_probe, 631 | .remove = ata_platform_remove_one, 632 | }; 633 | module_platform_driver(pata_gpio_driver); 634 | 635 | MODULE_DESCRIPTION("PATA driver using generic bitbanged GPIO"); 636 | MODULE_AUTHOR("Tobias Schramm, Tobias Maedel"); 637 | MODULE_LICENSE("GPL"); 638 | MODULE_VERSION("0.1"); -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | rmmod pata-gpio || true 6 | make 7 | insmod pata-gpio.ko 8 | --------------------------------------------------------------------------------