pata_bf54x.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. * File: drivers/ata/pata_bf54x.c
  3. * Author: Sonic Zhang <sonic.zhang@analog.com>
  4. *
  5. * Created:
  6. * Description: PATA Driver for blackfin 54x
  7. *
  8. * Modified:
  9. * Copyright 2007 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see the file COPYING, or write
  25. * to the Free Software Foundation, Inc.,
  26. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/pci.h>
  31. #include <linux/init.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/delay.h>
  34. #include <linux/device.h>
  35. #include <scsi/scsi_host.h>
  36. #include <linux/libata.h>
  37. #include <linux/platform_device.h>
  38. #include <asm/dma.h>
  39. #include <asm/gpio.h>
  40. #include <asm/portmux.h>
  41. #define DRV_NAME "pata-bf54x"
  42. #define DRV_VERSION "0.9"
  43. #define ATA_REG_CTRL 0x0E
  44. #define ATA_REG_ALTSTATUS ATA_REG_CTRL
  45. /* These are the offset of the controller's registers */
  46. #define ATAPI_OFFSET_CONTROL 0x00
  47. #define ATAPI_OFFSET_STATUS 0x04
  48. #define ATAPI_OFFSET_DEV_ADDR 0x08
  49. #define ATAPI_OFFSET_DEV_TXBUF 0x0c
  50. #define ATAPI_OFFSET_DEV_RXBUF 0x10
  51. #define ATAPI_OFFSET_INT_MASK 0x14
  52. #define ATAPI_OFFSET_INT_STATUS 0x18
  53. #define ATAPI_OFFSET_XFER_LEN 0x1c
  54. #define ATAPI_OFFSET_LINE_STATUS 0x20
  55. #define ATAPI_OFFSET_SM_STATE 0x24
  56. #define ATAPI_OFFSET_TERMINATE 0x28
  57. #define ATAPI_OFFSET_PIO_TFRCNT 0x2c
  58. #define ATAPI_OFFSET_DMA_TFRCNT 0x30
  59. #define ATAPI_OFFSET_UMAIN_TFRCNT 0x34
  60. #define ATAPI_OFFSET_UDMAOUT_TFRCNT 0x38
  61. #define ATAPI_OFFSET_REG_TIM_0 0x40
  62. #define ATAPI_OFFSET_PIO_TIM_0 0x44
  63. #define ATAPI_OFFSET_PIO_TIM_1 0x48
  64. #define ATAPI_OFFSET_MULTI_TIM_0 0x50
  65. #define ATAPI_OFFSET_MULTI_TIM_1 0x54
  66. #define ATAPI_OFFSET_MULTI_TIM_2 0x58
  67. #define ATAPI_OFFSET_ULTRA_TIM_0 0x60
  68. #define ATAPI_OFFSET_ULTRA_TIM_1 0x64
  69. #define ATAPI_OFFSET_ULTRA_TIM_2 0x68
  70. #define ATAPI_OFFSET_ULTRA_TIM_3 0x6c
  71. #define ATAPI_GET_CONTROL(base)\
  72. bfin_read16(base + ATAPI_OFFSET_CONTROL)
  73. #define ATAPI_SET_CONTROL(base, val)\
  74. bfin_write16(base + ATAPI_OFFSET_CONTROL, val)
  75. #define ATAPI_GET_STATUS(base)\
  76. bfin_read16(base + ATAPI_OFFSET_STATUS)
  77. #define ATAPI_GET_DEV_ADDR(base)\
  78. bfin_read16(base + ATAPI_OFFSET_DEV_ADDR)
  79. #define ATAPI_SET_DEV_ADDR(base, val)\
  80. bfin_write16(base + ATAPI_OFFSET_DEV_ADDR, val)
  81. #define ATAPI_GET_DEV_TXBUF(base)\
  82. bfin_read16(base + ATAPI_OFFSET_DEV_TXBUF)
  83. #define ATAPI_SET_DEV_TXBUF(base, val)\
  84. bfin_write16(base + ATAPI_OFFSET_DEV_TXBUF, val)
  85. #define ATAPI_GET_DEV_RXBUF(base)\
  86. bfin_read16(base + ATAPI_OFFSET_DEV_RXBUF)
  87. #define ATAPI_SET_DEV_RXBUF(base, val)\
  88. bfin_write16(base + ATAPI_OFFSET_DEV_RXBUF, val)
  89. #define ATAPI_GET_INT_MASK(base)\
  90. bfin_read16(base + ATAPI_OFFSET_INT_MASK)
  91. #define ATAPI_SET_INT_MASK(base, val)\
  92. bfin_write16(base + ATAPI_OFFSET_INT_MASK, val)
  93. #define ATAPI_GET_INT_STATUS(base)\
  94. bfin_read16(base + ATAPI_OFFSET_INT_STATUS)
  95. #define ATAPI_SET_INT_STATUS(base, val)\
  96. bfin_write16(base + ATAPI_OFFSET_INT_STATUS, val)
  97. #define ATAPI_GET_XFER_LEN(base)\
  98. bfin_read16(base + ATAPI_OFFSET_XFER_LEN)
  99. #define ATAPI_SET_XFER_LEN(base, val)\
  100. bfin_write16(base + ATAPI_OFFSET_XFER_LEN, val)
  101. #define ATAPI_GET_LINE_STATUS(base)\
  102. bfin_read16(base + ATAPI_OFFSET_LINE_STATUS)
  103. #define ATAPI_GET_SM_STATE(base)\
  104. bfin_read16(base + ATAPI_OFFSET_SM_STATE)
  105. #define ATAPI_GET_TERMINATE(base)\
  106. bfin_read16(base + ATAPI_OFFSET_TERMINATE)
  107. #define ATAPI_SET_TERMINATE(base, val)\
  108. bfin_write16(base + ATAPI_OFFSET_TERMINATE, val)
  109. #define ATAPI_GET_PIO_TFRCNT(base)\
  110. bfin_read16(base + ATAPI_OFFSET_PIO_TFRCNT)
  111. #define ATAPI_GET_DMA_TFRCNT(base)\
  112. bfin_read16(base + ATAPI_OFFSET_DMA_TFRCNT)
  113. #define ATAPI_GET_UMAIN_TFRCNT(base)\
  114. bfin_read16(base + ATAPI_OFFSET_UMAIN_TFRCNT)
  115. #define ATAPI_GET_UDMAOUT_TFRCNT(base)\
  116. bfin_read16(base + ATAPI_OFFSET_UDMAOUT_TFRCNT)
  117. #define ATAPI_GET_REG_TIM_0(base)\
  118. bfin_read16(base + ATAPI_OFFSET_REG_TIM_0)
  119. #define ATAPI_SET_REG_TIM_0(base, val)\
  120. bfin_write16(base + ATAPI_OFFSET_REG_TIM_0, val)
  121. #define ATAPI_GET_PIO_TIM_0(base)\
  122. bfin_read16(base + ATAPI_OFFSET_PIO_TIM_0)
  123. #define ATAPI_SET_PIO_TIM_0(base, val)\
  124. bfin_write16(base + ATAPI_OFFSET_PIO_TIM_0, val)
  125. #define ATAPI_GET_PIO_TIM_1(base)\
  126. bfin_read16(base + ATAPI_OFFSET_PIO_TIM_1)
  127. #define ATAPI_SET_PIO_TIM_1(base, val)\
  128. bfin_write16(base + ATAPI_OFFSET_PIO_TIM_1, val)
  129. #define ATAPI_GET_MULTI_TIM_0(base)\
  130. bfin_read16(base + ATAPI_OFFSET_MULTI_TIM_0)
  131. #define ATAPI_SET_MULTI_TIM_0(base, val)\
  132. bfin_write16(base + ATAPI_OFFSET_MULTI_TIM_0, val)
  133. #define ATAPI_GET_MULTI_TIM_1(base)\
  134. bfin_read16(base + ATAPI_OFFSET_MULTI_TIM_1)
  135. #define ATAPI_SET_MULTI_TIM_1(base, val)\
  136. bfin_write16(base + ATAPI_OFFSET_MULTI_TIM_1, val)
  137. #define ATAPI_GET_MULTI_TIM_2(base)\
  138. bfin_read16(base + ATAPI_OFFSET_MULTI_TIM_2)
  139. #define ATAPI_SET_MULTI_TIM_2(base, val)\
  140. bfin_write16(base + ATAPI_OFFSET_MULTI_TIM_2, val)
  141. #define ATAPI_GET_ULTRA_TIM_0(base)\
  142. bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_0)
  143. #define ATAPI_SET_ULTRA_TIM_0(base, val)\
  144. bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_0, val)
  145. #define ATAPI_GET_ULTRA_TIM_1(base)\
  146. bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_1)
  147. #define ATAPI_SET_ULTRA_TIM_1(base, val)\
  148. bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_1, val)
  149. #define ATAPI_GET_ULTRA_TIM_2(base)\
  150. bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_2)
  151. #define ATAPI_SET_ULTRA_TIM_2(base, val)\
  152. bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_2, val)
  153. #define ATAPI_GET_ULTRA_TIM_3(base)\
  154. bfin_read16(base + ATAPI_OFFSET_ULTRA_TIM_3)
  155. #define ATAPI_SET_ULTRA_TIM_3(base, val)\
  156. bfin_write16(base + ATAPI_OFFSET_ULTRA_TIM_3, val)
  157. /**
  158. * PIO Mode - Frequency compatibility
  159. */
  160. /* mode: 0 1 2 3 4 */
  161. static const u32 pio_fsclk[] =
  162. { 33333333, 33333333, 33333333, 33333333, 33333333 };
  163. /**
  164. * MDMA Mode - Frequency compatibility
  165. */
  166. /* mode: 0 1 2 */
  167. static const u32 mdma_fsclk[] = { 33333333, 33333333, 33333333 };
  168. /**
  169. * UDMA Mode - Frequency compatibility
  170. *
  171. * UDMA5 - 100 MB/s - SCLK = 133 MHz
  172. * UDMA4 - 66 MB/s - SCLK >= 80 MHz
  173. * UDMA3 - 44.4 MB/s - SCLK >= 50 MHz
  174. * UDMA2 - 33 MB/s - SCLK >= 40 MHz
  175. */
  176. /* mode: 0 1 2 3 4 5 */
  177. static const u32 udma_fsclk[] =
  178. { 33333333, 33333333, 40000000, 50000000, 80000000, 133333333 };
  179. /**
  180. * Register transfer timing table
  181. */
  182. /* mode: 0 1 2 3 4 */
  183. /* Cycle Time */
  184. static const u32 reg_t0min[] = { 600, 383, 330, 180, 120 };
  185. /* DIOR/DIOW to end cycle */
  186. static const u32 reg_t2min[] = { 290, 290, 290, 70, 25 };
  187. /* DIOR/DIOW asserted pulse width */
  188. static const u32 reg_teocmin[] = { 290, 290, 290, 80, 70 };
  189. /**
  190. * PIO timing table
  191. */
  192. /* mode: 0 1 2 3 4 */
  193. /* Cycle Time */
  194. static const u32 pio_t0min[] = { 600, 383, 240, 180, 120 };
  195. /* Address valid to DIOR/DIORW */
  196. static const u32 pio_t1min[] = { 70, 50, 30, 30, 25 };
  197. /* DIOR/DIOW to end cycle */
  198. static const u32 pio_t2min[] = { 165, 125, 100, 80, 70 };
  199. /* DIOR/DIOW asserted pulse width */
  200. static const u32 pio_teocmin[] = { 165, 125, 100, 70, 25 };
  201. /* DIOW data hold */
  202. static const u32 pio_t4min[] = { 30, 20, 15, 10, 10 };
  203. /* ******************************************************************
  204. * Multiword DMA timing table
  205. * ******************************************************************
  206. */
  207. /* mode: 0 1 2 */
  208. /* Cycle Time */
  209. static const u32 mdma_t0min[] = { 480, 150, 120 };
  210. /* DIOR/DIOW asserted pulse width */
  211. static const u32 mdma_tdmin[] = { 215, 80, 70 };
  212. /* DMACK to read data released */
  213. static const u32 mdma_thmin[] = { 20, 15, 10 };
  214. /* DIOR/DIOW to DMACK hold */
  215. static const u32 mdma_tjmin[] = { 20, 5, 5 };
  216. /* DIOR negated pulse width */
  217. static const u32 mdma_tkrmin[] = { 50, 50, 25 };
  218. /* DIOR negated pulse width */
  219. static const u32 mdma_tkwmin[] = { 215, 50, 25 };
  220. /* CS[1:0] valid to DIOR/DIOW */
  221. static const u32 mdma_tmmin[] = { 50, 30, 25 };
  222. /* DMACK to read data released */
  223. static const u32 mdma_tzmax[] = { 20, 25, 25 };
  224. /**
  225. * Ultra DMA timing table
  226. */
  227. /* mode: 0 1 2 3 4 5 */
  228. static const u32 udma_tcycmin[] = { 112, 73, 54, 39, 25, 17 };
  229. static const u32 udma_tdvsmin[] = { 70, 48, 31, 20, 7, 5 };
  230. static const u32 udma_tenvmax[] = { 70, 70, 70, 55, 55, 50 };
  231. static const u32 udma_trpmin[] = { 160, 125, 100, 100, 100, 85 };
  232. static const u32 udma_tmin[] = { 5, 5, 5, 5, 3, 3 };
  233. static const u32 udma_tmlimin = 20;
  234. static const u32 udma_tzahmin = 20;
  235. static const u32 udma_tenvmin = 20;
  236. static const u32 udma_tackmin = 20;
  237. static const u32 udma_tssmin = 50;
  238. /**
  239. *
  240. * Function: num_clocks_min
  241. *
  242. * Description:
  243. * calculate number of SCLK cycles to meet minimum timing
  244. */
  245. static unsigned short num_clocks_min(unsigned long tmin,
  246. unsigned long fsclk)
  247. {
  248. unsigned long tmp ;
  249. unsigned short result;
  250. tmp = tmin * (fsclk/1000/1000) / 1000;
  251. result = (unsigned short)tmp;
  252. if ((tmp*1000*1000) < (tmin*(fsclk/1000))) {
  253. result++;
  254. }
  255. return result;
  256. }
  257. /**
  258. * bfin_set_piomode - Initialize host controller PATA PIO timings
  259. * @ap: Port whose timings we are configuring
  260. * @adev: um
  261. *
  262. * Set PIO mode for device.
  263. *
  264. * LOCKING:
  265. * None (inherited from caller).
  266. */
  267. static void bfin_set_piomode(struct ata_port *ap, struct ata_device *adev)
  268. {
  269. int mode = adev->pio_mode - XFER_PIO_0;
  270. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  271. unsigned int fsclk = get_sclk();
  272. unsigned short teoc_reg, t2_reg, teoc_pio;
  273. unsigned short t4_reg, t2_pio, t1_reg;
  274. unsigned short n0, n6, t6min = 5;
  275. /* the most restrictive timing value is t6 and tc, the DIOW - data hold
  276. * If one SCLK pulse is longer than this minimum value then register
  277. * transfers cannot be supported at this frequency.
  278. */
  279. n6 = num_clocks_min(t6min, fsclk);
  280. if (mode >= 0 && mode <= 4 && n6 >= 1) {
  281. pr_debug("set piomode: mode=%d, fsclk=%ud\n", mode, fsclk);
  282. /* calculate the timing values for register transfers. */
  283. while (mode > 0 && pio_fsclk[mode] > fsclk)
  284. mode--;
  285. /* DIOR/DIOW to end cycle time */
  286. t2_reg = num_clocks_min(reg_t2min[mode], fsclk);
  287. /* DIOR/DIOW asserted pulse width */
  288. teoc_reg = num_clocks_min(reg_teocmin[mode], fsclk);
  289. /* Cycle Time */
  290. n0 = num_clocks_min(reg_t0min[mode], fsclk);
  291. /* increase t2 until we meed the minimum cycle length */
  292. if (t2_reg + teoc_reg < n0)
  293. t2_reg = n0 - teoc_reg;
  294. /* calculate the timing values for pio transfers. */
  295. /* DIOR/DIOW to end cycle time */
  296. t2_pio = num_clocks_min(pio_t2min[mode], fsclk);
  297. /* DIOR/DIOW asserted pulse width */
  298. teoc_pio = num_clocks_min(pio_teocmin[mode], fsclk);
  299. /* Cycle Time */
  300. n0 = num_clocks_min(pio_t0min[mode], fsclk);
  301. /* increase t2 until we meed the minimum cycle length */
  302. if (t2_pio + teoc_pio < n0)
  303. t2_pio = n0 - teoc_pio;
  304. /* Address valid to DIOR/DIORW */
  305. t1_reg = num_clocks_min(pio_t1min[mode], fsclk);
  306. /* DIOW data hold */
  307. t4_reg = num_clocks_min(pio_t4min[mode], fsclk);
  308. ATAPI_SET_REG_TIM_0(base, (teoc_reg<<8 | t2_reg));
  309. ATAPI_SET_PIO_TIM_0(base, (t4_reg<<12 | t2_pio<<4 | t1_reg));
  310. ATAPI_SET_PIO_TIM_1(base, teoc_pio);
  311. if (mode > 2) {
  312. ATAPI_SET_CONTROL(base,
  313. ATAPI_GET_CONTROL(base) | IORDY_EN);
  314. } else {
  315. ATAPI_SET_CONTROL(base,
  316. ATAPI_GET_CONTROL(base) & ~IORDY_EN);
  317. }
  318. /* Disable host ATAPI PIO interrupts */
  319. ATAPI_SET_INT_MASK(base, ATAPI_GET_INT_MASK(base)
  320. & ~(PIO_DONE_MASK | HOST_TERM_XFER_MASK));
  321. SSYNC();
  322. }
  323. }
  324. /**
  325. * bfin_set_dmamode - Initialize host controller PATA DMA timings
  326. * @ap: Port whose timings we are configuring
  327. * @adev: um
  328. * @udma: udma mode, 0 - 6
  329. *
  330. * Set UDMA mode for device.
  331. *
  332. * LOCKING:
  333. * None (inherited from caller).
  334. */
  335. static void bfin_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  336. {
  337. int mode;
  338. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  339. unsigned long fsclk = get_sclk();
  340. unsigned short tenv, tack, tcyc_tdvs, tdvs, tmli, tss, trp, tzah;
  341. unsigned short tm, td, tkr, tkw, teoc, th;
  342. unsigned short n0, nf, tfmin = 5;
  343. unsigned short nmin, tcyc;
  344. mode = adev->dma_mode - XFER_UDMA_0;
  345. if (mode >= 0 && mode <= 5) {
  346. pr_debug("set udmamode: mode=%d\n", mode);
  347. /* the most restrictive timing value is t6 and tc,
  348. * the DIOW - data hold. If one SCLK pulse is longer
  349. * than this minimum value then register
  350. * transfers cannot be supported at this frequency.
  351. */
  352. while (mode > 0 && udma_fsclk[mode] > fsclk)
  353. mode--;
  354. nmin = num_clocks_min(udma_tmin[mode], fsclk);
  355. if (nmin >= 1) {
  356. /* calculate the timing values for Ultra DMA. */
  357. tdvs = num_clocks_min(udma_tdvsmin[mode], fsclk);
  358. tcyc = num_clocks_min(udma_tcycmin[mode], fsclk);
  359. tcyc_tdvs = 2;
  360. /* increase tcyc - tdvs (tcyc_tdvs) until we meed
  361. * the minimum cycle length
  362. */
  363. if (tdvs + tcyc_tdvs < tcyc)
  364. tcyc_tdvs = tcyc - tdvs;
  365. /* Mow assign the values required for the timing
  366. * registers
  367. */
  368. if (tcyc_tdvs < 2)
  369. tcyc_tdvs = 2;
  370. if (tdvs < 2)
  371. tdvs = 2;
  372. tack = num_clocks_min(udma_tackmin, fsclk);
  373. tss = num_clocks_min(udma_tssmin, fsclk);
  374. tmli = num_clocks_min(udma_tmlimin, fsclk);
  375. tzah = num_clocks_min(udma_tzahmin, fsclk);
  376. trp = num_clocks_min(udma_trpmin[mode], fsclk);
  377. tenv = num_clocks_min(udma_tenvmin, fsclk);
  378. if (tenv <= udma_tenvmax[mode]) {
  379. ATAPI_SET_ULTRA_TIM_0(base, (tenv<<8 | tack));
  380. ATAPI_SET_ULTRA_TIM_1(base,
  381. (tcyc_tdvs<<8 | tdvs));
  382. ATAPI_SET_ULTRA_TIM_2(base, (tmli<<8 | tss));
  383. ATAPI_SET_ULTRA_TIM_3(base, (trp<<8 | tzah));
  384. /* Enable host ATAPI Untra DMA interrupts */
  385. ATAPI_SET_INT_MASK(base,
  386. ATAPI_GET_INT_MASK(base)
  387. | UDMAIN_DONE_MASK
  388. | UDMAOUT_DONE_MASK
  389. | UDMAIN_TERM_MASK
  390. | UDMAOUT_TERM_MASK);
  391. }
  392. }
  393. }
  394. mode = adev->dma_mode - XFER_MW_DMA_0;
  395. if (mode >= 0 && mode <= 2) {
  396. pr_debug("set mdmamode: mode=%d\n", mode);
  397. /* the most restrictive timing value is tf, the DMACK to
  398. * read data released. If one SCLK pulse is longer than
  399. * this maximum value then the MDMA mode
  400. * cannot be supported at this frequency.
  401. */
  402. while (mode > 0 && mdma_fsclk[mode] > fsclk)
  403. mode--;
  404. nf = num_clocks_min(tfmin, fsclk);
  405. if (nf >= 1) {
  406. /* calculate the timing values for Multi-word DMA. */
  407. /* DIOR/DIOW asserted pulse width */
  408. td = num_clocks_min(mdma_tdmin[mode], fsclk);
  409. /* DIOR negated pulse width */
  410. tkw = num_clocks_min(mdma_tkwmin[mode], fsclk);
  411. /* Cycle Time */
  412. n0 = num_clocks_min(mdma_t0min[mode], fsclk);
  413. /* increase tk until we meed the minimum cycle length */
  414. if (tkw + td < n0)
  415. tkw = n0 - td;
  416. /* DIOR negated pulse width - read */
  417. tkr = num_clocks_min(mdma_tkrmin[mode], fsclk);
  418. /* CS{1:0] valid to DIOR/DIOW */
  419. tm = num_clocks_min(mdma_tmmin[mode], fsclk);
  420. /* DIOR/DIOW to DMACK hold */
  421. teoc = num_clocks_min(mdma_tjmin[mode], fsclk);
  422. /* DIOW Data hold */
  423. th = num_clocks_min(mdma_thmin[mode], fsclk);
  424. ATAPI_SET_MULTI_TIM_0(base, (tm<<8 | td));
  425. ATAPI_SET_MULTI_TIM_1(base, (tkr<<8 | tkw));
  426. ATAPI_SET_MULTI_TIM_2(base, (teoc<<8 | th));
  427. /* Enable host ATAPI Multi DMA interrupts */
  428. ATAPI_SET_INT_MASK(base, ATAPI_GET_INT_MASK(base)
  429. | MULTI_DONE_MASK | MULTI_TERM_MASK);
  430. SSYNC();
  431. }
  432. }
  433. return;
  434. }
  435. /**
  436. *
  437. * Function: wait_complete
  438. *
  439. * Description: Waits the interrupt from device
  440. *
  441. */
  442. static inline void wait_complete(void __iomem *base, unsigned short mask)
  443. {
  444. unsigned short status;
  445. unsigned int i = 0;
  446. #define PATA_BF54X_WAIT_TIMEOUT 10000
  447. for (i = 0; i < PATA_BF54X_WAIT_TIMEOUT; i++) {
  448. status = ATAPI_GET_INT_STATUS(base) & mask;
  449. if (status)
  450. break;
  451. }
  452. ATAPI_SET_INT_STATUS(base, mask);
  453. }
  454. /**
  455. *
  456. * Function: write_atapi_register
  457. *
  458. * Description: Writes to ATA Device Resgister
  459. *
  460. */
  461. static void write_atapi_register(void __iomem *base,
  462. unsigned long ata_reg, unsigned short value)
  463. {
  464. /* Program the ATA_DEV_TXBUF register with write data (to be
  465. * written into the device).
  466. */
  467. ATAPI_SET_DEV_TXBUF(base, value);
  468. /* Program the ATA_DEV_ADDR register with address of the
  469. * device register (0x01 to 0x0F).
  470. */
  471. ATAPI_SET_DEV_ADDR(base, ata_reg);
  472. /* Program the ATA_CTRL register with dir set to write (1)
  473. */
  474. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  475. /* ensure PIO DMA is not set */
  476. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  477. /* and start the transfer */
  478. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  479. /* Wait for the interrupt to indicate the end of the transfer.
  480. * (We need to wait on and clear rhe ATA_DEV_INT interrupt status)
  481. */
  482. wait_complete(base, PIO_DONE_INT);
  483. }
  484. /**
  485. *
  486. * Function: read_atapi_register
  487. *
  488. *Description: Reads from ATA Device Resgister
  489. *
  490. */
  491. static unsigned short read_atapi_register(void __iomem *base,
  492. unsigned long ata_reg)
  493. {
  494. /* Program the ATA_DEV_ADDR register with address of the
  495. * device register (0x01 to 0x0F).
  496. */
  497. ATAPI_SET_DEV_ADDR(base, ata_reg);
  498. /* Program the ATA_CTRL register with dir set to read (0) and
  499. */
  500. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  501. /* ensure PIO DMA is not set */
  502. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  503. /* and start the transfer */
  504. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  505. /* Wait for the interrupt to indicate the end of the transfer.
  506. * (PIO_DONE interrupt is set and it doesn't seem to matter
  507. * that we don't clear it)
  508. */
  509. wait_complete(base, PIO_DONE_INT);
  510. /* Read the ATA_DEV_RXBUF register with write data (to be
  511. * written into the device).
  512. */
  513. return ATAPI_GET_DEV_RXBUF(base);
  514. }
  515. /**
  516. *
  517. * Function: write_atapi_register_data
  518. *
  519. * Description: Writes to ATA Device Resgister
  520. *
  521. */
  522. static void write_atapi_data(void __iomem *base,
  523. int len, unsigned short *buf)
  524. {
  525. int i;
  526. /* Set transfer length to 1 */
  527. ATAPI_SET_XFER_LEN(base, 1);
  528. /* Program the ATA_DEV_ADDR register with address of the
  529. * ATA_REG_DATA
  530. */
  531. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  532. /* Program the ATA_CTRL register with dir set to write (1)
  533. */
  534. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  535. /* ensure PIO DMA is not set */
  536. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  537. for (i = 0; i < len; i++) {
  538. /* Program the ATA_DEV_TXBUF register with write data (to be
  539. * written into the device).
  540. */
  541. ATAPI_SET_DEV_TXBUF(base, buf[i]);
  542. /* and start the transfer */
  543. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  544. /* Wait for the interrupt to indicate the end of the transfer.
  545. * (We need to wait on and clear rhe ATA_DEV_INT
  546. * interrupt status)
  547. */
  548. wait_complete(base, PIO_DONE_INT);
  549. }
  550. }
  551. /**
  552. *
  553. * Function: read_atapi_register_data
  554. *
  555. * Description: Reads from ATA Device Resgister
  556. *
  557. */
  558. static void read_atapi_data(void __iomem *base,
  559. int len, unsigned short *buf)
  560. {
  561. int i;
  562. /* Set transfer length to 1 */
  563. ATAPI_SET_XFER_LEN(base, 1);
  564. /* Program the ATA_DEV_ADDR register with address of the
  565. * ATA_REG_DATA
  566. */
  567. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  568. /* Program the ATA_CTRL register with dir set to read (0) and
  569. */
  570. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  571. /* ensure PIO DMA is not set */
  572. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  573. for (i = 0; i < len; i++) {
  574. /* and start the transfer */
  575. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  576. /* Wait for the interrupt to indicate the end of the transfer.
  577. * (PIO_DONE interrupt is set and it doesn't seem to matter
  578. * that we don't clear it)
  579. */
  580. wait_complete(base, PIO_DONE_INT);
  581. /* Read the ATA_DEV_RXBUF register with write data (to be
  582. * written into the device).
  583. */
  584. buf[i] = ATAPI_GET_DEV_RXBUF(base);
  585. }
  586. }
  587. /**
  588. * bfin_tf_load - send taskfile registers to host controller
  589. * @ap: Port to which output is sent
  590. * @tf: ATA taskfile register set
  591. *
  592. * Note: Original code is ata_tf_load().
  593. */
  594. static void bfin_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  595. {
  596. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  597. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  598. if (tf->ctl != ap->last_ctl) {
  599. write_atapi_register(base, ATA_REG_CTRL, tf->ctl);
  600. ap->last_ctl = tf->ctl;
  601. ata_wait_idle(ap);
  602. }
  603. if (is_addr) {
  604. if (tf->flags & ATA_TFLAG_LBA48) {
  605. write_atapi_register(base, ATA_REG_FEATURE,
  606. tf->hob_feature);
  607. write_atapi_register(base, ATA_REG_NSECT,
  608. tf->hob_nsect);
  609. write_atapi_register(base, ATA_REG_LBAL, tf->hob_lbal);
  610. write_atapi_register(base, ATA_REG_LBAM, tf->hob_lbam);
  611. write_atapi_register(base, ATA_REG_LBAH, tf->hob_lbah);
  612. pr_debug("hob: feat 0x%X nsect 0x%X, lba 0x%X "
  613. "0x%X 0x%X\n",
  614. tf->hob_feature,
  615. tf->hob_nsect,
  616. tf->hob_lbal,
  617. tf->hob_lbam,
  618. tf->hob_lbah);
  619. }
  620. write_atapi_register(base, ATA_REG_FEATURE, tf->feature);
  621. write_atapi_register(base, ATA_REG_NSECT, tf->nsect);
  622. write_atapi_register(base, ATA_REG_LBAL, tf->lbal);
  623. write_atapi_register(base, ATA_REG_LBAM, tf->lbam);
  624. write_atapi_register(base, ATA_REG_LBAH, tf->lbah);
  625. pr_debug("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  626. tf->feature,
  627. tf->nsect,
  628. tf->lbal,
  629. tf->lbam,
  630. tf->lbah);
  631. }
  632. if (tf->flags & ATA_TFLAG_DEVICE) {
  633. write_atapi_register(base, ATA_REG_DEVICE, tf->device);
  634. pr_debug("device 0x%X\n", tf->device);
  635. }
  636. ata_wait_idle(ap);
  637. }
  638. /**
  639. * bfin_check_status - Read device status reg & clear interrupt
  640. * @ap: port where the device is
  641. *
  642. * Note: Original code is ata_check_status().
  643. */
  644. static u8 bfin_check_status(struct ata_port *ap)
  645. {
  646. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  647. return read_atapi_register(base, ATA_REG_STATUS);
  648. }
  649. /**
  650. * bfin_tf_read - input device's ATA taskfile shadow registers
  651. * @ap: Port from which input is read
  652. * @tf: ATA taskfile register set for storing input
  653. *
  654. * Note: Original code is ata_tf_read().
  655. */
  656. static void bfin_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  657. {
  658. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  659. tf->command = bfin_check_status(ap);
  660. tf->feature = read_atapi_register(base, ATA_REG_ERR);
  661. tf->nsect = read_atapi_register(base, ATA_REG_NSECT);
  662. tf->lbal = read_atapi_register(base, ATA_REG_LBAL);
  663. tf->lbam = read_atapi_register(base, ATA_REG_LBAM);
  664. tf->lbah = read_atapi_register(base, ATA_REG_LBAH);
  665. tf->device = read_atapi_register(base, ATA_REG_DEVICE);
  666. if (tf->flags & ATA_TFLAG_LBA48) {
  667. write_atapi_register(base, ATA_REG_CTRL, tf->ctl | ATA_HOB);
  668. tf->hob_feature = read_atapi_register(base, ATA_REG_ERR);
  669. tf->hob_nsect = read_atapi_register(base, ATA_REG_NSECT);
  670. tf->hob_lbal = read_atapi_register(base, ATA_REG_LBAL);
  671. tf->hob_lbam = read_atapi_register(base, ATA_REG_LBAM);
  672. tf->hob_lbah = read_atapi_register(base, ATA_REG_LBAH);
  673. }
  674. }
  675. /**
  676. * bfin_exec_command - issue ATA command to host controller
  677. * @ap: port to which command is being issued
  678. * @tf: ATA taskfile register set
  679. *
  680. * Note: Original code is ata_exec_command().
  681. */
  682. static void bfin_exec_command(struct ata_port *ap,
  683. const struct ata_taskfile *tf)
  684. {
  685. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  686. pr_debug("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  687. write_atapi_register(base, ATA_REG_CMD, tf->command);
  688. ata_pause(ap);
  689. }
  690. /**
  691. * bfin_check_altstatus - Read device alternate status reg
  692. * @ap: port where the device is
  693. */
  694. static u8 bfin_check_altstatus(struct ata_port *ap)
  695. {
  696. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  697. return read_atapi_register(base, ATA_REG_ALTSTATUS);
  698. }
  699. /**
  700. * bfin_std_dev_select - Select device 0/1 on ATA bus
  701. * @ap: ATA channel to manipulate
  702. * @device: ATA device (numbered from zero) to select
  703. *
  704. * Note: Original code is ata_std_dev_select().
  705. */
  706. static void bfin_std_dev_select(struct ata_port *ap, unsigned int device)
  707. {
  708. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  709. u8 tmp;
  710. if (device == 0)
  711. tmp = ATA_DEVICE_OBS;
  712. else
  713. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  714. write_atapi_register(base, ATA_REG_DEVICE, tmp);
  715. ata_pause(ap);
  716. }
  717. /**
  718. * bfin_bmdma_setup - Set up IDE DMA transaction
  719. * @qc: Info associated with this ATA transaction.
  720. *
  721. * Note: Original code is ata_bmdma_setup().
  722. */
  723. static void bfin_bmdma_setup(struct ata_queued_cmd *qc)
  724. {
  725. unsigned short config = WDSIZE_16;
  726. struct scatterlist *sg;
  727. pr_debug("in atapi dma setup\n");
  728. /* Program the ATA_CTRL register with dir */
  729. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  730. /* fill the ATAPI DMA controller */
  731. set_dma_config(CH_ATAPI_TX, config);
  732. set_dma_x_modify(CH_ATAPI_TX, 2);
  733. ata_for_each_sg(sg, qc) {
  734. set_dma_start_addr(CH_ATAPI_TX, sg_dma_address(sg));
  735. set_dma_x_count(CH_ATAPI_TX, sg_dma_len(sg) >> 1);
  736. }
  737. } else {
  738. config |= WNR;
  739. /* fill the ATAPI DMA controller */
  740. set_dma_config(CH_ATAPI_RX, config);
  741. set_dma_x_modify(CH_ATAPI_RX, 2);
  742. ata_for_each_sg(sg, qc) {
  743. set_dma_start_addr(CH_ATAPI_RX, sg_dma_address(sg));
  744. set_dma_x_count(CH_ATAPI_RX, sg_dma_len(sg) >> 1);
  745. }
  746. }
  747. }
  748. /**
  749. * bfin_bmdma_start - Start an IDE DMA transaction
  750. * @qc: Info associated with this ATA transaction.
  751. *
  752. * Note: Original code is ata_bmdma_start().
  753. */
  754. static void bfin_bmdma_start(struct ata_queued_cmd *qc)
  755. {
  756. struct ata_port *ap = qc->ap;
  757. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  758. struct scatterlist *sg;
  759. pr_debug("in atapi dma start\n");
  760. if (!(ap->udma_mask || ap->mwdma_mask))
  761. return;
  762. /* start ATAPI DMA controller*/
  763. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  764. /*
  765. * On blackfin arch, uncacheable memory is not
  766. * allocated with flag GFP_DMA. DMA buffer from
  767. * common kenel code should be flushed if WB
  768. * data cache is enabled. Otherwise, this loop
  769. * is an empty loop and optimized out.
  770. */
  771. ata_for_each_sg(sg, qc) {
  772. flush_dcache_range(sg_dma_address(sg),
  773. sg_dma_address(sg) + sg_dma_len(sg));
  774. }
  775. enable_dma(CH_ATAPI_TX);
  776. pr_debug("enable udma write\n");
  777. /* Send ATA DMA write command */
  778. bfin_exec_command(ap, &qc->tf);
  779. /* set ATA DMA write direction */
  780. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base)
  781. | XFER_DIR));
  782. } else {
  783. enable_dma(CH_ATAPI_RX);
  784. pr_debug("enable udma read\n");
  785. /* Send ATA DMA read command */
  786. bfin_exec_command(ap, &qc->tf);
  787. /* set ATA DMA read direction */
  788. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base)
  789. & ~XFER_DIR));
  790. }
  791. /* Reset all transfer count */
  792. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | TFRCNT_RST);
  793. /* Set transfer length to buffer len */
  794. ata_for_each_sg(sg, qc) {
  795. ATAPI_SET_XFER_LEN(base, (sg_dma_len(sg) >> 1));
  796. }
  797. /* Enable ATA DMA operation*/
  798. if (ap->udma_mask)
  799. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base)
  800. | ULTRA_START);
  801. else
  802. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base)
  803. | MULTI_START);
  804. }
  805. /**
  806. * bfin_bmdma_stop - Stop IDE DMA transfer
  807. * @qc: Command we are ending DMA for
  808. */
  809. static void bfin_bmdma_stop(struct ata_queued_cmd *qc)
  810. {
  811. struct ata_port *ap = qc->ap;
  812. struct scatterlist *sg;
  813. pr_debug("in atapi dma stop\n");
  814. if (!(ap->udma_mask || ap->mwdma_mask))
  815. return;
  816. /* stop ATAPI DMA controller*/
  817. if (qc->tf.flags & ATA_TFLAG_WRITE)
  818. disable_dma(CH_ATAPI_TX);
  819. else {
  820. disable_dma(CH_ATAPI_RX);
  821. if (ap->hsm_task_state & HSM_ST_LAST) {
  822. /*
  823. * On blackfin arch, uncacheable memory is not
  824. * allocated with flag GFP_DMA. DMA buffer from
  825. * common kenel code should be invalidated if
  826. * data cache is enabled. Otherwise, this loop
  827. * is an empty loop and optimized out.
  828. */
  829. ata_for_each_sg(sg, qc) {
  830. invalidate_dcache_range(
  831. sg_dma_address(sg),
  832. sg_dma_address(sg)
  833. + sg_dma_len(sg));
  834. }
  835. }
  836. }
  837. }
  838. /**
  839. * bfin_devchk - PATA device presence detection
  840. * @ap: ATA channel to examine
  841. * @device: Device to examine (starting at zero)
  842. *
  843. * Note: Original code is ata_devchk().
  844. */
  845. static unsigned int bfin_devchk(struct ata_port *ap,
  846. unsigned int device)
  847. {
  848. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  849. u8 nsect, lbal;
  850. bfin_std_dev_select(ap, device);
  851. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  852. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  853. write_atapi_register(base, ATA_REG_NSECT, 0xaa);
  854. write_atapi_register(base, ATA_REG_LBAL, 0x55);
  855. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  856. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  857. nsect = read_atapi_register(base, ATA_REG_NSECT);
  858. lbal = read_atapi_register(base, ATA_REG_LBAL);
  859. if ((nsect == 0x55) && (lbal == 0xaa))
  860. return 1; /* we found a device */
  861. return 0; /* nothing found */
  862. }
  863. /**
  864. * bfin_bus_post_reset - PATA device post reset
  865. *
  866. * Note: Original code is ata_bus_post_reset().
  867. */
  868. static void bfin_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  869. {
  870. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  871. unsigned int dev0 = devmask & (1 << 0);
  872. unsigned int dev1 = devmask & (1 << 1);
  873. unsigned long timeout;
  874. /* if device 0 was found in ata_devchk, wait for its
  875. * BSY bit to clear
  876. */
  877. if (dev0)
  878. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  879. /* if device 1 was found in ata_devchk, wait for
  880. * register access, then wait for BSY to clear
  881. */
  882. timeout = jiffies + ATA_TMOUT_BOOT;
  883. while (dev1) {
  884. u8 nsect, lbal;
  885. bfin_std_dev_select(ap, 1);
  886. nsect = read_atapi_register(base, ATA_REG_NSECT);
  887. lbal = read_atapi_register(base, ATA_REG_LBAL);
  888. if ((nsect == 1) && (lbal == 1))
  889. break;
  890. if (time_after(jiffies, timeout)) {
  891. dev1 = 0;
  892. break;
  893. }
  894. msleep(50); /* give drive a breather */
  895. }
  896. if (dev1)
  897. ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  898. /* is all this really necessary? */
  899. bfin_std_dev_select(ap, 0);
  900. if (dev1)
  901. bfin_std_dev_select(ap, 1);
  902. if (dev0)
  903. bfin_std_dev_select(ap, 0);
  904. }
  905. /**
  906. * bfin_bus_softreset - PATA device software reset
  907. *
  908. * Note: Original code is ata_bus_softreset().
  909. */
  910. static unsigned int bfin_bus_softreset(struct ata_port *ap,
  911. unsigned int devmask)
  912. {
  913. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  914. /* software reset. causes dev0 to be selected */
  915. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  916. udelay(20);
  917. write_atapi_register(base, ATA_REG_CTRL, ap->ctl | ATA_SRST);
  918. udelay(20);
  919. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  920. /* spec mandates ">= 2ms" before checking status.
  921. * We wait 150ms, because that was the magic delay used for
  922. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  923. * between when the ATA command register is written, and then
  924. * status is checked. Because waiting for "a while" before
  925. * checking status is fine, post SRST, we perform this magic
  926. * delay here as well.
  927. *
  928. * Old drivers/ide uses the 2mS rule and then waits for ready
  929. */
  930. msleep(150);
  931. /* Before we perform post reset processing we want to see if
  932. * the bus shows 0xFF because the odd clown forgets the D7
  933. * pulldown resistor.
  934. */
  935. if (bfin_check_status(ap) == 0xFF)
  936. return 0;
  937. bfin_bus_post_reset(ap, devmask);
  938. return 0;
  939. }
  940. /**
  941. * bfin_std_softreset - reset host port via ATA SRST
  942. * @ap: port to reset
  943. * @classes: resulting classes of attached devices
  944. *
  945. * Note: Original code is ata_std_softreset().
  946. */
  947. static int bfin_std_softreset(struct ata_link *link, unsigned int *classes,
  948. unsigned long deadline)
  949. {
  950. struct ata_port *ap = link->ap;
  951. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  952. unsigned int devmask = 0, err_mask;
  953. u8 err;
  954. if (ata_link_offline(link)) {
  955. classes[0] = ATA_DEV_NONE;
  956. goto out;
  957. }
  958. /* determine if device 0/1 are present */
  959. if (bfin_devchk(ap, 0))
  960. devmask |= (1 << 0);
  961. if (slave_possible && bfin_devchk(ap, 1))
  962. devmask |= (1 << 1);
  963. /* select device 0 again */
  964. bfin_std_dev_select(ap, 0);
  965. /* issue bus reset */
  966. err_mask = bfin_bus_softreset(ap, devmask);
  967. if (err_mask) {
  968. ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
  969. err_mask);
  970. return -EIO;
  971. }
  972. /* determine by signature whether we have ATA or ATAPI devices */
  973. classes[0] = ata_dev_try_classify(&ap->link.device[0],
  974. devmask & (1 << 0), &err);
  975. if (slave_possible && err != 0x81)
  976. classes[1] = ata_dev_try_classify(&ap->link.device[1],
  977. devmask & (1 << 1), &err);
  978. out:
  979. return 0;
  980. }
  981. /**
  982. * bfin_bmdma_status - Read IDE DMA status
  983. * @ap: Port associated with this ATA transaction.
  984. */
  985. static unsigned char bfin_bmdma_status(struct ata_port *ap)
  986. {
  987. unsigned char host_stat = 0;
  988. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  989. unsigned short int_status = ATAPI_GET_INT_STATUS(base);
  990. if (ATAPI_GET_STATUS(base) & (MULTI_XFER_ON|ULTRA_XFER_ON)) {
  991. host_stat = ATA_DMA_ACTIVE;
  992. }
  993. if (int_status & (MULTI_DONE_INT|UDMAIN_DONE_INT|UDMAOUT_DONE_INT)) {
  994. host_stat = ATA_DMA_INTR;
  995. }
  996. if (int_status & (MULTI_TERM_INT|UDMAIN_TERM_INT|UDMAOUT_TERM_INT)) {
  997. host_stat = ATA_DMA_ERR;
  998. }
  999. return host_stat;
  1000. }
  1001. /**
  1002. * bfin_data_xfer - Transfer data by PIO
  1003. * @adev: device for this I/O
  1004. * @buf: data buffer
  1005. * @buflen: buffer length
  1006. * @write_data: read/write
  1007. *
  1008. * Note: Original code is ata_data_xfer().
  1009. */
  1010. static void bfin_data_xfer(struct ata_device *adev, unsigned char *buf,
  1011. unsigned int buflen, int write_data)
  1012. {
  1013. struct ata_port *ap = adev->link->ap;
  1014. unsigned int words = buflen >> 1;
  1015. unsigned short *buf16 = (u16 *) buf;
  1016. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1017. /* Transfer multiple of 2 bytes */
  1018. if (write_data) {
  1019. write_atapi_data(base, words, buf16);
  1020. } else {
  1021. read_atapi_data(base, words, buf16);
  1022. }
  1023. /* Transfer trailing 1 byte, if any. */
  1024. if (unlikely(buflen & 0x01)) {
  1025. unsigned short align_buf[1] = { 0 };
  1026. unsigned char *trailing_buf = buf + buflen - 1;
  1027. if (write_data) {
  1028. memcpy(align_buf, trailing_buf, 1);
  1029. write_atapi_data(base, 1, align_buf);
  1030. } else {
  1031. read_atapi_data(base, 1, align_buf);
  1032. memcpy(trailing_buf, align_buf, 1);
  1033. }
  1034. }
  1035. }
  1036. /**
  1037. * bfin_irq_clear - Clear ATAPI interrupt.
  1038. * @ap: Port associated with this ATA transaction.
  1039. *
  1040. * Note: Original code is ata_bmdma_irq_clear().
  1041. */
  1042. static void bfin_irq_clear(struct ata_port *ap)
  1043. {
  1044. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1045. pr_debug("in atapi irq clear\n");
  1046. ATAPI_SET_INT_STATUS(base, ATAPI_GET_INT_STATUS(base)|ATAPI_DEV_INT
  1047. | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
  1048. | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT);
  1049. }
  1050. /**
  1051. * bfin_irq_on - Enable interrupts on a port.
  1052. * @ap: Port on which interrupts are enabled.
  1053. *
  1054. * Note: Original code is ata_irq_on().
  1055. */
  1056. static unsigned char bfin_irq_on(struct ata_port *ap)
  1057. {
  1058. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1059. u8 tmp;
  1060. pr_debug("in atapi irq on\n");
  1061. ap->ctl &= ~ATA_NIEN;
  1062. ap->last_ctl = ap->ctl;
  1063. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  1064. tmp = ata_wait_idle(ap);
  1065. bfin_irq_clear(ap);
  1066. return tmp;
  1067. }
  1068. /**
  1069. * bfin_bmdma_freeze - Freeze DMA controller port
  1070. * @ap: port to freeze
  1071. *
  1072. * Note: Original code is ata_bmdma_freeze().
  1073. */
  1074. static void bfin_bmdma_freeze(struct ata_port *ap)
  1075. {
  1076. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1077. pr_debug("in atapi dma freeze\n");
  1078. ap->ctl |= ATA_NIEN;
  1079. ap->last_ctl = ap->ctl;
  1080. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  1081. /* Under certain circumstances, some controllers raise IRQ on
  1082. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1083. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1084. */
  1085. ata_chk_status(ap);
  1086. bfin_irq_clear(ap);
  1087. }
  1088. /**
  1089. * bfin_bmdma_thaw - Thaw DMA controller port
  1090. * @ap: port to thaw
  1091. *
  1092. * Note: Original code is ata_bmdma_thaw().
  1093. */
  1094. void bfin_bmdma_thaw(struct ata_port *ap)
  1095. {
  1096. bfin_check_status(ap);
  1097. bfin_irq_clear(ap);
  1098. bfin_irq_on(ap);
  1099. }
  1100. /**
  1101. * bfin_std_postreset - standard postreset callback
  1102. * @ap: the target ata_port
  1103. * @classes: classes of attached devices
  1104. *
  1105. * Note: Original code is ata_std_postreset().
  1106. */
  1107. static void bfin_std_postreset(struct ata_link *link, unsigned int *classes)
  1108. {
  1109. struct ata_port *ap = link->ap;
  1110. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1111. /* re-enable interrupts */
  1112. bfin_irq_on(ap);
  1113. /* is double-select really necessary? */
  1114. if (classes[0] != ATA_DEV_NONE)
  1115. bfin_std_dev_select(ap, 1);
  1116. if (classes[1] != ATA_DEV_NONE)
  1117. bfin_std_dev_select(ap, 0);
  1118. /* bail out if no device is present */
  1119. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1120. return;
  1121. }
  1122. /* set up device control */
  1123. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  1124. }
  1125. /**
  1126. * bfin_error_handler - Stock error handler for DMA controller
  1127. * @ap: port to handle error for
  1128. */
  1129. static void bfin_error_handler(struct ata_port *ap)
  1130. {
  1131. ata_bmdma_drive_eh(ap, ata_std_prereset, bfin_std_softreset, NULL,
  1132. bfin_std_postreset);
  1133. }
  1134. static void bfin_port_stop(struct ata_port *ap)
  1135. {
  1136. pr_debug("in atapi port stop\n");
  1137. if (ap->udma_mask != 0 || ap->mwdma_mask != 0) {
  1138. free_dma(CH_ATAPI_RX);
  1139. free_dma(CH_ATAPI_TX);
  1140. }
  1141. }
  1142. static int bfin_port_start(struct ata_port *ap)
  1143. {
  1144. pr_debug("in atapi port start\n");
  1145. if (!(ap->udma_mask || ap->mwdma_mask))
  1146. return 0;
  1147. if (request_dma(CH_ATAPI_RX, "BFIN ATAPI RX DMA") >= 0) {
  1148. if (request_dma(CH_ATAPI_TX,
  1149. "BFIN ATAPI TX DMA") >= 0)
  1150. return 0;
  1151. free_dma(CH_ATAPI_RX);
  1152. }
  1153. ap->udma_mask = 0;
  1154. ap->mwdma_mask = 0;
  1155. dev_err(ap->dev, "Unable to request ATAPI DMA!"
  1156. " Continue in PIO mode.\n");
  1157. return 0;
  1158. }
  1159. static struct scsi_host_template bfin_sht = {
  1160. .module = THIS_MODULE,
  1161. .name = DRV_NAME,
  1162. .ioctl = ata_scsi_ioctl,
  1163. .queuecommand = ata_scsi_queuecmd,
  1164. .can_queue = ATA_DEF_QUEUE,
  1165. .this_id = ATA_SHT_THIS_ID,
  1166. .sg_tablesize = SG_NONE,
  1167. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  1168. .emulated = ATA_SHT_EMULATED,
  1169. .use_clustering = ATA_SHT_USE_CLUSTERING,
  1170. .proc_name = DRV_NAME,
  1171. .dma_boundary = ATA_DMA_BOUNDARY,
  1172. .slave_configure = ata_scsi_slave_config,
  1173. .slave_destroy = ata_scsi_slave_destroy,
  1174. .bios_param = ata_std_bios_param,
  1175. #ifdef CONFIG_PM
  1176. .resume = ata_scsi_device_resume,
  1177. .suspend = ata_scsi_device_suspend,
  1178. #endif
  1179. };
  1180. static const struct ata_port_operations bfin_pata_ops = {
  1181. .set_piomode = bfin_set_piomode,
  1182. .set_dmamode = bfin_set_dmamode,
  1183. .tf_load = bfin_tf_load,
  1184. .tf_read = bfin_tf_read,
  1185. .exec_command = bfin_exec_command,
  1186. .check_status = bfin_check_status,
  1187. .check_altstatus = bfin_check_altstatus,
  1188. .dev_select = bfin_std_dev_select,
  1189. .bmdma_setup = bfin_bmdma_setup,
  1190. .bmdma_start = bfin_bmdma_start,
  1191. .bmdma_stop = bfin_bmdma_stop,
  1192. .bmdma_status = bfin_bmdma_status,
  1193. .data_xfer = bfin_data_xfer,
  1194. .qc_prep = ata_noop_qc_prep,
  1195. .qc_issue = ata_qc_issue_prot,
  1196. .freeze = bfin_bmdma_freeze,
  1197. .thaw = bfin_bmdma_thaw,
  1198. .error_handler = bfin_error_handler,
  1199. .post_internal_cmd = bfin_bmdma_stop,
  1200. .irq_handler = ata_interrupt,
  1201. .irq_clear = bfin_irq_clear,
  1202. .irq_on = bfin_irq_on,
  1203. .port_start = bfin_port_start,
  1204. .port_stop = bfin_port_stop,
  1205. };
  1206. static struct ata_port_info bfin_port_info[] = {
  1207. {
  1208. .sht = &bfin_sht,
  1209. .flags = ATA_FLAG_SLAVE_POSS
  1210. | ATA_FLAG_MMIO
  1211. | ATA_FLAG_NO_LEGACY,
  1212. .pio_mask = 0x1f, /* pio0-4 */
  1213. .mwdma_mask = 0,
  1214. .udma_mask = 0,
  1215. .port_ops = &bfin_pata_ops,
  1216. },
  1217. };
  1218. /**
  1219. * bfin_reset_controller - initialize BF54x ATAPI controller.
  1220. */
  1221. static int bfin_reset_controller(struct ata_host *host)
  1222. {
  1223. void __iomem *base = (void __iomem *)host->ports[0]->ioaddr.ctl_addr;
  1224. int count;
  1225. unsigned short status;
  1226. /* Disable all ATAPI interrupts */
  1227. ATAPI_SET_INT_MASK(base, 0);
  1228. SSYNC();
  1229. /* Assert the RESET signal 25us*/
  1230. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | DEV_RST);
  1231. udelay(30);
  1232. /* Negate the RESET signal for 2ms*/
  1233. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) & ~DEV_RST);
  1234. msleep(2);
  1235. /* Wait on Busy flag to clear */
  1236. count = 10000000;
  1237. do {
  1238. status = read_atapi_register(base, ATA_REG_STATUS);
  1239. } while (count-- && (status & ATA_BUSY));
  1240. /* Enable only ATAPI Device interrupt */
  1241. ATAPI_SET_INT_MASK(base, 1);
  1242. SSYNC();
  1243. return (!count);
  1244. }
  1245. /**
  1246. * atapi_io_port - define atapi peripheral port pins.
  1247. */
  1248. static unsigned short atapi_io_port[] = {
  1249. P_ATAPI_RESET,
  1250. P_ATAPI_DIOR,
  1251. P_ATAPI_DIOW,
  1252. P_ATAPI_CS0,
  1253. P_ATAPI_CS1,
  1254. P_ATAPI_DMACK,
  1255. P_ATAPI_DMARQ,
  1256. P_ATAPI_INTRQ,
  1257. P_ATAPI_IORDY,
  1258. 0
  1259. };
  1260. /**
  1261. * bfin_atapi_probe - attach a bfin atapi interface
  1262. * @pdev: platform device
  1263. *
  1264. * Register a bfin atapi interface.
  1265. *
  1266. *
  1267. * Platform devices are expected to contain 2 resources per port:
  1268. *
  1269. * - I/O Base (IORESOURCE_IO)
  1270. * - IRQ (IORESOURCE_IRQ)
  1271. *
  1272. */
  1273. static int __devinit bfin_atapi_probe(struct platform_device *pdev)
  1274. {
  1275. int board_idx = 0;
  1276. struct resource *res;
  1277. struct ata_host *host;
  1278. const struct ata_port_info *ppi[] =
  1279. { &bfin_port_info[board_idx], NULL };
  1280. /*
  1281. * Simple resource validation ..
  1282. */
  1283. if (unlikely(pdev->num_resources != 2)) {
  1284. dev_err(&pdev->dev, "invalid number of resources\n");
  1285. return -EINVAL;
  1286. }
  1287. /*
  1288. * Get the register base first
  1289. */
  1290. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1291. if (res == NULL)
  1292. return -EINVAL;
  1293. /*
  1294. * Now that that's out of the way, wire up the port..
  1295. */
  1296. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 1);
  1297. if (!host)
  1298. return -ENOMEM;
  1299. host->ports[0]->ioaddr.ctl_addr = (void *)res->start;
  1300. if (peripheral_request_list(atapi_io_port, "atapi-io-port")) {
  1301. dev_err(&pdev->dev, "Requesting Peripherals faild\n");
  1302. return -EFAULT;
  1303. }
  1304. if (bfin_reset_controller(host)) {
  1305. peripheral_free_list(atapi_io_port);
  1306. dev_err(&pdev->dev, "Fail to reset ATAPI device\n");
  1307. return -EFAULT;
  1308. }
  1309. if (ata_host_activate(host, platform_get_irq(pdev, 0),
  1310. ata_interrupt, IRQF_SHARED, &bfin_sht) != 0) {
  1311. peripheral_free_list(atapi_io_port);
  1312. dev_err(&pdev->dev, "Fail to attach ATAPI device\n");
  1313. return -ENODEV;
  1314. }
  1315. return 0;
  1316. }
  1317. /**
  1318. * bfin_atapi_remove - unplug a bfin atapi interface
  1319. * @pdev: platform device
  1320. *
  1321. * A bfin atapi device has been unplugged. Perform the needed
  1322. * cleanup. Also called on module unload for any active devices.
  1323. */
  1324. static int __devexit bfin_atapi_remove(struct platform_device *pdev)
  1325. {
  1326. struct device *dev = &pdev->dev;
  1327. struct ata_host *host = dev_get_drvdata(dev);
  1328. ata_host_detach(host);
  1329. peripheral_free_list(atapi_io_port);
  1330. return 0;
  1331. }
  1332. #ifdef CONFIG_PM
  1333. int bfin_atapi_suspend(struct platform_device *pdev, pm_message_t state)
  1334. {
  1335. return 0;
  1336. }
  1337. int bfin_atapi_resume(struct platform_device *pdev)
  1338. {
  1339. return 0;
  1340. }
  1341. #endif
  1342. static struct platform_driver bfin_atapi_driver = {
  1343. .probe = bfin_atapi_probe,
  1344. .remove = __devexit_p(bfin_atapi_remove),
  1345. .driver = {
  1346. .name = DRV_NAME,
  1347. .owner = THIS_MODULE,
  1348. #ifdef CONFIG_PM
  1349. .suspend = bfin_atapi_suspend,
  1350. .resume = bfin_atapi_resume,
  1351. #endif
  1352. },
  1353. };
  1354. #define ATAPI_MODE_SIZE 10
  1355. static char bfin_atapi_mode[ATAPI_MODE_SIZE];
  1356. static int __init bfin_atapi_init(void)
  1357. {
  1358. pr_info("register bfin atapi driver\n");
  1359. switch(bfin_atapi_mode[0]) {
  1360. case 'p':
  1361. case 'P':
  1362. break;
  1363. case 'm':
  1364. case 'M':
  1365. bfin_port_info[0].mwdma_mask = ATA_MWDMA2;
  1366. break;
  1367. default:
  1368. bfin_port_info[0].udma_mask = ATA_UDMA5;
  1369. };
  1370. return platform_driver_register(&bfin_atapi_driver);
  1371. }
  1372. static void __exit bfin_atapi_exit(void)
  1373. {
  1374. platform_driver_unregister(&bfin_atapi_driver);
  1375. }
  1376. module_init(bfin_atapi_init);
  1377. module_exit(bfin_atapi_exit);
  1378. /*
  1379. * ATAPI mode:
  1380. * pio/PIO
  1381. * udma/UDMA (default)
  1382. * mwdma/MWDMA
  1383. */
  1384. module_param_string(bfin_atapi_mode, bfin_atapi_mode, ATAPI_MODE_SIZE, 0);
  1385. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  1386. MODULE_DESCRIPTION("PATA driver for blackfin 54x ATAPI controller");
  1387. MODULE_LICENSE("GPL");
  1388. MODULE_VERSION(DRV_VERSION);