pata_bf54x.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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. dev_dbg(adev->link->ap->dev, "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. *
  329. * Set UDMA mode for device.
  330. *
  331. * LOCKING:
  332. * None (inherited from caller).
  333. */
  334. static void bfin_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  335. {
  336. int mode;
  337. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  338. unsigned long fsclk = get_sclk();
  339. unsigned short tenv, tack, tcyc_tdvs, tdvs, tmli, tss, trp, tzah;
  340. unsigned short tm, td, tkr, tkw, teoc, th;
  341. unsigned short n0, nf, tfmin = 5;
  342. unsigned short nmin, tcyc;
  343. mode = adev->dma_mode - XFER_UDMA_0;
  344. if (mode >= 0 && mode <= 5) {
  345. dev_dbg(adev->link->ap->dev, "set udmamode: mode=%d\n", mode);
  346. /* the most restrictive timing value is t6 and tc,
  347. * the DIOW - data hold. If one SCLK pulse is longer
  348. * than this minimum value then register
  349. * transfers cannot be supported at this frequency.
  350. */
  351. while (mode > 0 && udma_fsclk[mode] > fsclk)
  352. mode--;
  353. nmin = num_clocks_min(udma_tmin[mode], fsclk);
  354. if (nmin >= 1) {
  355. /* calculate the timing values for Ultra DMA. */
  356. tdvs = num_clocks_min(udma_tdvsmin[mode], fsclk);
  357. tcyc = num_clocks_min(udma_tcycmin[mode], fsclk);
  358. tcyc_tdvs = 2;
  359. /* increase tcyc - tdvs (tcyc_tdvs) until we meed
  360. * the minimum cycle length
  361. */
  362. if (tdvs + tcyc_tdvs < tcyc)
  363. tcyc_tdvs = tcyc - tdvs;
  364. /* Mow assign the values required for the timing
  365. * registers
  366. */
  367. if (tcyc_tdvs < 2)
  368. tcyc_tdvs = 2;
  369. if (tdvs < 2)
  370. tdvs = 2;
  371. tack = num_clocks_min(udma_tackmin, fsclk);
  372. tss = num_clocks_min(udma_tssmin, fsclk);
  373. tmli = num_clocks_min(udma_tmlimin, fsclk);
  374. tzah = num_clocks_min(udma_tzahmin, fsclk);
  375. trp = num_clocks_min(udma_trpmin[mode], fsclk);
  376. tenv = num_clocks_min(udma_tenvmin, fsclk);
  377. if (tenv <= udma_tenvmax[mode]) {
  378. ATAPI_SET_ULTRA_TIM_0(base, (tenv<<8 | tack));
  379. ATAPI_SET_ULTRA_TIM_1(base,
  380. (tcyc_tdvs<<8 | tdvs));
  381. ATAPI_SET_ULTRA_TIM_2(base, (tmli<<8 | tss));
  382. ATAPI_SET_ULTRA_TIM_3(base, (trp<<8 | tzah));
  383. /* Enable host ATAPI Untra DMA interrupts */
  384. ATAPI_SET_INT_MASK(base,
  385. ATAPI_GET_INT_MASK(base)
  386. | UDMAIN_DONE_MASK
  387. | UDMAOUT_DONE_MASK
  388. | UDMAIN_TERM_MASK
  389. | UDMAOUT_TERM_MASK);
  390. }
  391. }
  392. }
  393. mode = adev->dma_mode - XFER_MW_DMA_0;
  394. if (mode >= 0 && mode <= 2) {
  395. dev_dbg(adev->link->ap->dev, "set mdmamode: mode=%d\n", mode);
  396. /* the most restrictive timing value is tf, the DMACK to
  397. * read data released. If one SCLK pulse is longer than
  398. * this maximum value then the MDMA mode
  399. * cannot be supported at this frequency.
  400. */
  401. while (mode > 0 && mdma_fsclk[mode] > fsclk)
  402. mode--;
  403. nf = num_clocks_min(tfmin, fsclk);
  404. if (nf >= 1) {
  405. /* calculate the timing values for Multi-word DMA. */
  406. /* DIOR/DIOW asserted pulse width */
  407. td = num_clocks_min(mdma_tdmin[mode], fsclk);
  408. /* DIOR negated pulse width */
  409. tkw = num_clocks_min(mdma_tkwmin[mode], fsclk);
  410. /* Cycle Time */
  411. n0 = num_clocks_min(mdma_t0min[mode], fsclk);
  412. /* increase tk until we meed the minimum cycle length */
  413. if (tkw + td < n0)
  414. tkw = n0 - td;
  415. /* DIOR negated pulse width - read */
  416. tkr = num_clocks_min(mdma_tkrmin[mode], fsclk);
  417. /* CS{1:0] valid to DIOR/DIOW */
  418. tm = num_clocks_min(mdma_tmmin[mode], fsclk);
  419. /* DIOR/DIOW to DMACK hold */
  420. teoc = num_clocks_min(mdma_tjmin[mode], fsclk);
  421. /* DIOW Data hold */
  422. th = num_clocks_min(mdma_thmin[mode], fsclk);
  423. ATAPI_SET_MULTI_TIM_0(base, (tm<<8 | td));
  424. ATAPI_SET_MULTI_TIM_1(base, (tkr<<8 | tkw));
  425. ATAPI_SET_MULTI_TIM_2(base, (teoc<<8 | th));
  426. /* Enable host ATAPI Multi DMA interrupts */
  427. ATAPI_SET_INT_MASK(base, ATAPI_GET_INT_MASK(base)
  428. | MULTI_DONE_MASK | MULTI_TERM_MASK);
  429. SSYNC();
  430. }
  431. }
  432. return;
  433. }
  434. /**
  435. *
  436. * Function: wait_complete
  437. *
  438. * Description: Waits the interrupt from device
  439. *
  440. */
  441. static inline void wait_complete(void __iomem *base, unsigned short mask)
  442. {
  443. unsigned short status;
  444. unsigned int i = 0;
  445. #define PATA_BF54X_WAIT_TIMEOUT 10000
  446. for (i = 0; i < PATA_BF54X_WAIT_TIMEOUT; i++) {
  447. status = ATAPI_GET_INT_STATUS(base) & mask;
  448. if (status)
  449. break;
  450. }
  451. ATAPI_SET_INT_STATUS(base, mask);
  452. }
  453. /**
  454. *
  455. * Function: write_atapi_register
  456. *
  457. * Description: Writes to ATA Device Resgister
  458. *
  459. */
  460. static void write_atapi_register(void __iomem *base,
  461. unsigned long ata_reg, unsigned short value)
  462. {
  463. /* Program the ATA_DEV_TXBUF register with write data (to be
  464. * written into the device).
  465. */
  466. ATAPI_SET_DEV_TXBUF(base, value);
  467. /* Program the ATA_DEV_ADDR register with address of the
  468. * device register (0x01 to 0x0F).
  469. */
  470. ATAPI_SET_DEV_ADDR(base, ata_reg);
  471. /* Program the ATA_CTRL register with dir set to write (1)
  472. */
  473. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  474. /* ensure PIO DMA is not set */
  475. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  476. /* and start the transfer */
  477. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  478. /* Wait for the interrupt to indicate the end of the transfer.
  479. * (We need to wait on and clear rhe ATA_DEV_INT interrupt status)
  480. */
  481. wait_complete(base, PIO_DONE_INT);
  482. }
  483. /**
  484. *
  485. * Function: read_atapi_register
  486. *
  487. *Description: Reads from ATA Device Resgister
  488. *
  489. */
  490. static unsigned short read_atapi_register(void __iomem *base,
  491. unsigned long ata_reg)
  492. {
  493. /* Program the ATA_DEV_ADDR register with address of the
  494. * device register (0x01 to 0x0F).
  495. */
  496. ATAPI_SET_DEV_ADDR(base, ata_reg);
  497. /* Program the ATA_CTRL register with dir set to read (0) and
  498. */
  499. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  500. /* ensure PIO DMA is not set */
  501. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  502. /* and start the transfer */
  503. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  504. /* Wait for the interrupt to indicate the end of the transfer.
  505. * (PIO_DONE interrupt is set and it doesn't seem to matter
  506. * that we don't clear it)
  507. */
  508. wait_complete(base, PIO_DONE_INT);
  509. /* Read the ATA_DEV_RXBUF register with write data (to be
  510. * written into the device).
  511. */
  512. return ATAPI_GET_DEV_RXBUF(base);
  513. }
  514. /**
  515. *
  516. * Function: write_atapi_register_data
  517. *
  518. * Description: Writes to ATA Device Resgister
  519. *
  520. */
  521. static void write_atapi_data(void __iomem *base,
  522. int len, unsigned short *buf)
  523. {
  524. int i;
  525. /* Set transfer length to 1 */
  526. ATAPI_SET_XFER_LEN(base, 1);
  527. /* Program the ATA_DEV_ADDR register with address of the
  528. * ATA_REG_DATA
  529. */
  530. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  531. /* Program the ATA_CTRL register with dir set to write (1)
  532. */
  533. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  534. /* ensure PIO DMA is not set */
  535. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  536. for (i = 0; i < len; i++) {
  537. /* Program the ATA_DEV_TXBUF register with write data (to be
  538. * written into the device).
  539. */
  540. ATAPI_SET_DEV_TXBUF(base, buf[i]);
  541. /* and start the transfer */
  542. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  543. /* Wait for the interrupt to indicate the end of the transfer.
  544. * (We need to wait on and clear rhe ATA_DEV_INT
  545. * interrupt status)
  546. */
  547. wait_complete(base, PIO_DONE_INT);
  548. }
  549. }
  550. /**
  551. *
  552. * Function: read_atapi_register_data
  553. *
  554. * Description: Reads from ATA Device Resgister
  555. *
  556. */
  557. static void read_atapi_data(void __iomem *base,
  558. int len, unsigned short *buf)
  559. {
  560. int i;
  561. /* Set transfer length to 1 */
  562. ATAPI_SET_XFER_LEN(base, 1);
  563. /* Program the ATA_DEV_ADDR register with address of the
  564. * ATA_REG_DATA
  565. */
  566. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  567. /* Program the ATA_CTRL register with dir set to read (0) and
  568. */
  569. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  570. /* ensure PIO DMA is not set */
  571. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  572. for (i = 0; i < len; i++) {
  573. /* and start the transfer */
  574. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  575. /* Wait for the interrupt to indicate the end of the transfer.
  576. * (PIO_DONE interrupt is set and it doesn't seem to matter
  577. * that we don't clear it)
  578. */
  579. wait_complete(base, PIO_DONE_INT);
  580. /* Read the ATA_DEV_RXBUF register with write data (to be
  581. * written into the device).
  582. */
  583. buf[i] = ATAPI_GET_DEV_RXBUF(base);
  584. }
  585. }
  586. /**
  587. * bfin_tf_load - send taskfile registers to host controller
  588. * @ap: Port to which output is sent
  589. * @tf: ATA taskfile register set
  590. *
  591. * Note: Original code is ata_sff_tf_load().
  592. */
  593. static void bfin_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  594. {
  595. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  596. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  597. if (tf->ctl != ap->last_ctl) {
  598. write_atapi_register(base, ATA_REG_CTRL, tf->ctl);
  599. ap->last_ctl = tf->ctl;
  600. ata_wait_idle(ap);
  601. }
  602. if (is_addr) {
  603. if (tf->flags & ATA_TFLAG_LBA48) {
  604. write_atapi_register(base, ATA_REG_FEATURE,
  605. tf->hob_feature);
  606. write_atapi_register(base, ATA_REG_NSECT,
  607. tf->hob_nsect);
  608. write_atapi_register(base, ATA_REG_LBAL, tf->hob_lbal);
  609. write_atapi_register(base, ATA_REG_LBAM, tf->hob_lbam);
  610. write_atapi_register(base, ATA_REG_LBAH, tf->hob_lbah);
  611. dev_dbg(ap->dev, "hob: feat 0x%X nsect 0x%X, lba 0x%X "
  612. "0x%X 0x%X\n",
  613. tf->hob_feature,
  614. tf->hob_nsect,
  615. tf->hob_lbal,
  616. tf->hob_lbam,
  617. tf->hob_lbah);
  618. }
  619. write_atapi_register(base, ATA_REG_FEATURE, tf->feature);
  620. write_atapi_register(base, ATA_REG_NSECT, tf->nsect);
  621. write_atapi_register(base, ATA_REG_LBAL, tf->lbal);
  622. write_atapi_register(base, ATA_REG_LBAM, tf->lbam);
  623. write_atapi_register(base, ATA_REG_LBAH, tf->lbah);
  624. dev_dbg(ap->dev, "feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  625. tf->feature,
  626. tf->nsect,
  627. tf->lbal,
  628. tf->lbam,
  629. tf->lbah);
  630. }
  631. if (tf->flags & ATA_TFLAG_DEVICE) {
  632. write_atapi_register(base, ATA_REG_DEVICE, tf->device);
  633. dev_dbg(ap->dev, "device 0x%X\n", tf->device);
  634. }
  635. ata_wait_idle(ap);
  636. }
  637. /**
  638. * bfin_check_status - Read device status reg & clear interrupt
  639. * @ap: port where the device is
  640. *
  641. * Note: Original code is ata_check_status().
  642. */
  643. static u8 bfin_check_status(struct ata_port *ap)
  644. {
  645. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  646. return read_atapi_register(base, ATA_REG_STATUS);
  647. }
  648. /**
  649. * bfin_tf_read - input device's ATA taskfile shadow registers
  650. * @ap: Port from which input is read
  651. * @tf: ATA taskfile register set for storing input
  652. *
  653. * Note: Original code is ata_sff_tf_read().
  654. */
  655. static void bfin_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  656. {
  657. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  658. tf->command = bfin_check_status(ap);
  659. tf->feature = read_atapi_register(base, ATA_REG_ERR);
  660. tf->nsect = read_atapi_register(base, ATA_REG_NSECT);
  661. tf->lbal = read_atapi_register(base, ATA_REG_LBAL);
  662. tf->lbam = read_atapi_register(base, ATA_REG_LBAM);
  663. tf->lbah = read_atapi_register(base, ATA_REG_LBAH);
  664. tf->device = read_atapi_register(base, ATA_REG_DEVICE);
  665. if (tf->flags & ATA_TFLAG_LBA48) {
  666. write_atapi_register(base, ATA_REG_CTRL, tf->ctl | ATA_HOB);
  667. tf->hob_feature = read_atapi_register(base, ATA_REG_ERR);
  668. tf->hob_nsect = read_atapi_register(base, ATA_REG_NSECT);
  669. tf->hob_lbal = read_atapi_register(base, ATA_REG_LBAL);
  670. tf->hob_lbam = read_atapi_register(base, ATA_REG_LBAM);
  671. tf->hob_lbah = read_atapi_register(base, ATA_REG_LBAH);
  672. }
  673. }
  674. /**
  675. * bfin_exec_command - issue ATA command to host controller
  676. * @ap: port to which command is being issued
  677. * @tf: ATA taskfile register set
  678. *
  679. * Note: Original code is ata_sff_exec_command().
  680. */
  681. static void bfin_exec_command(struct ata_port *ap,
  682. const struct ata_taskfile *tf)
  683. {
  684. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  685. dev_dbg(ap->dev, "ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  686. write_atapi_register(base, ATA_REG_CMD, tf->command);
  687. ata_sff_pause(ap);
  688. }
  689. /**
  690. * bfin_check_altstatus - Read device alternate status reg
  691. * @ap: port where the device is
  692. */
  693. static u8 bfin_check_altstatus(struct ata_port *ap)
  694. {
  695. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  696. return read_atapi_register(base, ATA_REG_ALTSTATUS);
  697. }
  698. /**
  699. * bfin_dev_select - Select device 0/1 on ATA bus
  700. * @ap: ATA channel to manipulate
  701. * @device: ATA device (numbered from zero) to select
  702. *
  703. * Note: Original code is ata_sff_dev_select().
  704. */
  705. static void bfin_dev_select(struct ata_port *ap, unsigned int device)
  706. {
  707. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  708. u8 tmp;
  709. if (device == 0)
  710. tmp = ATA_DEVICE_OBS;
  711. else
  712. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  713. write_atapi_register(base, ATA_REG_DEVICE, tmp);
  714. ata_sff_pause(ap);
  715. }
  716. /**
  717. * bfin_bmdma_setup - Set up IDE DMA transaction
  718. * @qc: Info associated with this ATA transaction.
  719. *
  720. * Note: Original code is ata_bmdma_setup().
  721. */
  722. static void bfin_bmdma_setup(struct ata_queued_cmd *qc)
  723. {
  724. unsigned short config = WDSIZE_16;
  725. struct scatterlist *sg;
  726. unsigned int si;
  727. dev_dbg(qc->ap->dev, "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. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  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. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  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. unsigned int si;
  760. dev_dbg(qc->ap->dev, "in atapi dma start\n");
  761. if (!(ap->udma_mask || ap->mwdma_mask))
  762. return;
  763. /* start ATAPI DMA controller*/
  764. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  765. /*
  766. * On blackfin arch, uncacheable memory is not
  767. * allocated with flag GFP_DMA. DMA buffer from
  768. * common kenel code should be flushed if WB
  769. * data cache is enabled. Otherwise, this loop
  770. * is an empty loop and optimized out.
  771. */
  772. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  773. flush_dcache_range(sg_dma_address(sg),
  774. sg_dma_address(sg) + sg_dma_len(sg));
  775. }
  776. enable_dma(CH_ATAPI_TX);
  777. dev_dbg(qc->ap->dev, "enable udma write\n");
  778. /* Send ATA DMA write command */
  779. bfin_exec_command(ap, &qc->tf);
  780. /* set ATA DMA write direction */
  781. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base)
  782. | XFER_DIR));
  783. } else {
  784. enable_dma(CH_ATAPI_RX);
  785. dev_dbg(qc->ap->dev, "enable udma read\n");
  786. /* Send ATA DMA read command */
  787. bfin_exec_command(ap, &qc->tf);
  788. /* set ATA DMA read direction */
  789. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base)
  790. & ~XFER_DIR));
  791. }
  792. /* Reset all transfer count */
  793. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | TFRCNT_RST);
  794. /* Set ATAPI state machine contorl in terminate sequence */
  795. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | END_ON_TERM);
  796. /* Set transfer length to buffer len */
  797. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  798. ATAPI_SET_XFER_LEN(base, (sg_dma_len(sg) >> 1));
  799. }
  800. /* Enable ATA DMA operation*/
  801. if (ap->udma_mask)
  802. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base)
  803. | ULTRA_START);
  804. else
  805. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base)
  806. | MULTI_START);
  807. }
  808. /**
  809. * bfin_bmdma_stop - Stop IDE DMA transfer
  810. * @qc: Command we are ending DMA for
  811. */
  812. static void bfin_bmdma_stop(struct ata_queued_cmd *qc)
  813. {
  814. struct ata_port *ap = qc->ap;
  815. struct scatterlist *sg;
  816. unsigned int si;
  817. dev_dbg(qc->ap->dev, "in atapi dma stop\n");
  818. if (!(ap->udma_mask || ap->mwdma_mask))
  819. return;
  820. /* stop ATAPI DMA controller*/
  821. if (qc->tf.flags & ATA_TFLAG_WRITE)
  822. disable_dma(CH_ATAPI_TX);
  823. else {
  824. disable_dma(CH_ATAPI_RX);
  825. if (ap->hsm_task_state & HSM_ST_LAST) {
  826. /*
  827. * On blackfin arch, uncacheable memory is not
  828. * allocated with flag GFP_DMA. DMA buffer from
  829. * common kenel code should be invalidated if
  830. * data cache is enabled. Otherwise, this loop
  831. * is an empty loop and optimized out.
  832. */
  833. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  834. invalidate_dcache_range(
  835. sg_dma_address(sg),
  836. sg_dma_address(sg)
  837. + sg_dma_len(sg));
  838. }
  839. }
  840. }
  841. }
  842. /**
  843. * bfin_devchk - PATA device presence detection
  844. * @ap: ATA channel to examine
  845. * @device: Device to examine (starting at zero)
  846. *
  847. * Note: Original code is ata_devchk().
  848. */
  849. static unsigned int bfin_devchk(struct ata_port *ap,
  850. unsigned int device)
  851. {
  852. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  853. u8 nsect, lbal;
  854. bfin_dev_select(ap, device);
  855. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  856. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  857. write_atapi_register(base, ATA_REG_NSECT, 0xaa);
  858. write_atapi_register(base, ATA_REG_LBAL, 0x55);
  859. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  860. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  861. nsect = read_atapi_register(base, ATA_REG_NSECT);
  862. lbal = read_atapi_register(base, ATA_REG_LBAL);
  863. if ((nsect == 0x55) && (lbal == 0xaa))
  864. return 1; /* we found a device */
  865. return 0; /* nothing found */
  866. }
  867. /**
  868. * bfin_bus_post_reset - PATA device post reset
  869. *
  870. * Note: Original code is ata_bus_post_reset().
  871. */
  872. static void bfin_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  873. {
  874. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  875. unsigned int dev0 = devmask & (1 << 0);
  876. unsigned int dev1 = devmask & (1 << 1);
  877. unsigned long deadline;
  878. /* if device 0 was found in ata_devchk, wait for its
  879. * BSY bit to clear
  880. */
  881. if (dev0)
  882. ata_sff_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  883. /* if device 1 was found in ata_devchk, wait for
  884. * register access, then wait for BSY to clear
  885. */
  886. deadline = ata_deadline(jiffies, ATA_TMOUT_BOOT);
  887. while (dev1) {
  888. u8 nsect, lbal;
  889. bfin_dev_select(ap, 1);
  890. nsect = read_atapi_register(base, ATA_REG_NSECT);
  891. lbal = read_atapi_register(base, ATA_REG_LBAL);
  892. if ((nsect == 1) && (lbal == 1))
  893. break;
  894. if (time_after(jiffies, deadline)) {
  895. dev1 = 0;
  896. break;
  897. }
  898. msleep(50); /* give drive a breather */
  899. }
  900. if (dev1)
  901. ata_sff_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  902. /* is all this really necessary? */
  903. bfin_dev_select(ap, 0);
  904. if (dev1)
  905. bfin_dev_select(ap, 1);
  906. if (dev0)
  907. bfin_dev_select(ap, 0);
  908. }
  909. /**
  910. * bfin_bus_softreset - PATA device software reset
  911. *
  912. * Note: Original code is ata_bus_softreset().
  913. */
  914. static unsigned int bfin_bus_softreset(struct ata_port *ap,
  915. unsigned int devmask)
  916. {
  917. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  918. /* software reset. causes dev0 to be selected */
  919. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  920. udelay(20);
  921. write_atapi_register(base, ATA_REG_CTRL, ap->ctl | ATA_SRST);
  922. udelay(20);
  923. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  924. /* spec mandates ">= 2ms" before checking status.
  925. * We wait 150ms, because that was the magic delay used for
  926. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  927. * between when the ATA command register is written, and then
  928. * status is checked. Because waiting for "a while" before
  929. * checking status is fine, post SRST, we perform this magic
  930. * delay here as well.
  931. *
  932. * Old drivers/ide uses the 2mS rule and then waits for ready
  933. */
  934. msleep(150);
  935. /* Before we perform post reset processing we want to see if
  936. * the bus shows 0xFF because the odd clown forgets the D7
  937. * pulldown resistor.
  938. */
  939. if (bfin_check_status(ap) == 0xFF)
  940. return 0;
  941. bfin_bus_post_reset(ap, devmask);
  942. return 0;
  943. }
  944. /**
  945. * bfin_softreset - reset host port via ATA SRST
  946. * @ap: port to reset
  947. * @classes: resulting classes of attached devices
  948. *
  949. * Note: Original code is ata_sff_softreset().
  950. */
  951. static int bfin_softreset(struct ata_link *link, unsigned int *classes,
  952. unsigned long deadline)
  953. {
  954. struct ata_port *ap = link->ap;
  955. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  956. unsigned int devmask = 0, err_mask;
  957. u8 err;
  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_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_sff_dev_classify(&ap->link.device[0],
  974. devmask & (1 << 0), &err);
  975. if (slave_possible && err != 0x81)
  976. classes[1] = ata_sff_dev_classify(&ap->link.device[1],
  977. devmask & (1 << 1), &err);
  978. return 0;
  979. }
  980. /**
  981. * bfin_bmdma_status - Read IDE DMA status
  982. * @ap: Port associated with this ATA transaction.
  983. */
  984. static unsigned char bfin_bmdma_status(struct ata_port *ap)
  985. {
  986. unsigned char host_stat = 0;
  987. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  988. unsigned short int_status = ATAPI_GET_INT_STATUS(base);
  989. if (ATAPI_GET_STATUS(base) & (MULTI_XFER_ON|ULTRA_XFER_ON))
  990. host_stat |= ATA_DMA_ACTIVE;
  991. if (int_status & (MULTI_DONE_INT|UDMAIN_DONE_INT|UDMAOUT_DONE_INT|
  992. ATAPI_DEV_INT))
  993. host_stat |= ATA_DMA_INTR;
  994. if (int_status & (MULTI_TERM_INT|UDMAIN_TERM_INT|UDMAOUT_TERM_INT))
  995. host_stat |= ATA_DMA_ERR|ATA_DMA_INTR;
  996. dev_dbg(ap->dev, "ATAPI: host_stat=0x%x\n", host_stat);
  997. return host_stat;
  998. }
  999. /**
  1000. * bfin_data_xfer - Transfer data by PIO
  1001. * @adev: device for this I/O
  1002. * @buf: data buffer
  1003. * @buflen: buffer length
  1004. * @write_data: read/write
  1005. *
  1006. * Note: Original code is ata_sff_data_xfer().
  1007. */
  1008. static unsigned int bfin_data_xfer(struct ata_device *dev, unsigned char *buf,
  1009. unsigned int buflen, int rw)
  1010. {
  1011. struct ata_port *ap = dev->link->ap;
  1012. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1013. unsigned int words = buflen >> 1;
  1014. unsigned short *buf16 = (u16 *)buf;
  1015. /* Transfer multiple of 2 bytes */
  1016. if (rw == READ)
  1017. read_atapi_data(base, words, buf16);
  1018. else
  1019. write_atapi_data(base, words, buf16);
  1020. /* Transfer trailing 1 byte, if any. */
  1021. if (unlikely(buflen & 0x01)) {
  1022. unsigned short align_buf[1] = { 0 };
  1023. unsigned char *trailing_buf = buf + buflen - 1;
  1024. if (rw == READ) {
  1025. read_atapi_data(base, 1, align_buf);
  1026. memcpy(trailing_buf, align_buf, 1);
  1027. } else {
  1028. memcpy(align_buf, trailing_buf, 1);
  1029. write_atapi_data(base, 1, align_buf);
  1030. }
  1031. words++;
  1032. }
  1033. return words << 1;
  1034. }
  1035. /**
  1036. * bfin_irq_clear - Clear ATAPI interrupt.
  1037. * @ap: Port associated with this ATA transaction.
  1038. *
  1039. * Note: Original code is ata_sff_irq_clear().
  1040. */
  1041. static void bfin_irq_clear(struct ata_port *ap)
  1042. {
  1043. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1044. dev_dbg(ap->dev, "in atapi irq clear\n");
  1045. ATAPI_SET_INT_STATUS(base, ATAPI_GET_INT_STATUS(base)|ATAPI_DEV_INT
  1046. | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
  1047. | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT);
  1048. }
  1049. /**
  1050. * bfin_irq_on - Enable interrupts on a port.
  1051. * @ap: Port on which interrupts are enabled.
  1052. *
  1053. * Note: Original code is ata_sff_irq_on().
  1054. */
  1055. static unsigned char bfin_irq_on(struct ata_port *ap)
  1056. {
  1057. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1058. u8 tmp;
  1059. dev_dbg(ap->dev, "in atapi irq on\n");
  1060. ap->ctl &= ~ATA_NIEN;
  1061. ap->last_ctl = ap->ctl;
  1062. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  1063. tmp = ata_wait_idle(ap);
  1064. bfin_irq_clear(ap);
  1065. return tmp;
  1066. }
  1067. /**
  1068. * bfin_freeze - Freeze DMA controller port
  1069. * @ap: port to freeze
  1070. *
  1071. * Note: Original code is ata_sff_freeze().
  1072. */
  1073. static void bfin_freeze(struct ata_port *ap)
  1074. {
  1075. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1076. dev_dbg(ap->dev, "in atapi dma freeze\n");
  1077. ap->ctl |= ATA_NIEN;
  1078. ap->last_ctl = ap->ctl;
  1079. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  1080. /* Under certain circumstances, some controllers raise IRQ on
  1081. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1082. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1083. */
  1084. ap->ops->sff_check_status(ap);
  1085. bfin_irq_clear(ap);
  1086. }
  1087. /**
  1088. * bfin_thaw - Thaw DMA controller port
  1089. * @ap: port to thaw
  1090. *
  1091. * Note: Original code is ata_sff_thaw().
  1092. */
  1093. void bfin_thaw(struct ata_port *ap)
  1094. {
  1095. dev_dbg(ap->dev, "in atapi dma thaw\n");
  1096. bfin_check_status(ap);
  1097. bfin_irq_on(ap);
  1098. }
  1099. /**
  1100. * bfin_postreset - standard postreset callback
  1101. * @ap: the target ata_port
  1102. * @classes: classes of attached devices
  1103. *
  1104. * Note: Original code is ata_sff_postreset().
  1105. */
  1106. static void bfin_postreset(struct ata_link *link, unsigned int *classes)
  1107. {
  1108. struct ata_port *ap = link->ap;
  1109. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  1110. /* re-enable interrupts */
  1111. bfin_irq_on(ap);
  1112. /* is double-select really necessary? */
  1113. if (classes[0] != ATA_DEV_NONE)
  1114. bfin_dev_select(ap, 1);
  1115. if (classes[1] != ATA_DEV_NONE)
  1116. bfin_dev_select(ap, 0);
  1117. /* bail out if no device is present */
  1118. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1119. return;
  1120. }
  1121. /* set up device control */
  1122. write_atapi_register(base, ATA_REG_CTRL, ap->ctl);
  1123. }
  1124. static void bfin_port_stop(struct ata_port *ap)
  1125. {
  1126. dev_dbg(ap->dev, "in atapi port stop\n");
  1127. if (ap->udma_mask != 0 || ap->mwdma_mask != 0) {
  1128. free_dma(CH_ATAPI_RX);
  1129. free_dma(CH_ATAPI_TX);
  1130. }
  1131. }
  1132. static int bfin_port_start(struct ata_port *ap)
  1133. {
  1134. dev_dbg(ap->dev, "in atapi port start\n");
  1135. if (!(ap->udma_mask || ap->mwdma_mask))
  1136. return 0;
  1137. if (request_dma(CH_ATAPI_RX, "BFIN ATAPI RX DMA") >= 0) {
  1138. if (request_dma(CH_ATAPI_TX,
  1139. "BFIN ATAPI TX DMA") >= 0)
  1140. return 0;
  1141. free_dma(CH_ATAPI_RX);
  1142. }
  1143. ap->udma_mask = 0;
  1144. ap->mwdma_mask = 0;
  1145. dev_err(ap->dev, "Unable to request ATAPI DMA!"
  1146. " Continue in PIO mode.\n");
  1147. return 0;
  1148. }
  1149. static unsigned int bfin_ata_host_intr(struct ata_port *ap,
  1150. struct ata_queued_cmd *qc)
  1151. {
  1152. struct ata_eh_info *ehi = &ap->link.eh_info;
  1153. u8 status, host_stat = 0;
  1154. VPRINTK("ata%u: protocol %d task_state %d\n",
  1155. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  1156. /* Check whether we are expecting interrupt in this state */
  1157. switch (ap->hsm_task_state) {
  1158. case HSM_ST_FIRST:
  1159. /* Some pre-ATAPI-4 devices assert INTRQ
  1160. * at this state when ready to receive CDB.
  1161. */
  1162. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  1163. * The flag was turned on only for atapi devices.
  1164. * No need to check is_atapi_taskfile(&qc->tf) again.
  1165. */
  1166. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1167. goto idle_irq;
  1168. break;
  1169. case HSM_ST_LAST:
  1170. if (qc->tf.protocol == ATA_PROT_DMA ||
  1171. qc->tf.protocol == ATAPI_PROT_DMA) {
  1172. /* check status of DMA engine */
  1173. host_stat = ap->ops->bmdma_status(ap);
  1174. VPRINTK("ata%u: host_stat 0x%X\n",
  1175. ap->print_id, host_stat);
  1176. /* if it's not our irq... */
  1177. if (!(host_stat & ATA_DMA_INTR))
  1178. goto idle_irq;
  1179. /* before we do anything else, clear DMA-Start bit */
  1180. ap->ops->bmdma_stop(qc);
  1181. if (unlikely(host_stat & ATA_DMA_ERR)) {
  1182. /* error when transfering data to/from memory */
  1183. qc->err_mask |= AC_ERR_HOST_BUS;
  1184. ap->hsm_task_state = HSM_ST_ERR;
  1185. }
  1186. }
  1187. break;
  1188. case HSM_ST:
  1189. break;
  1190. default:
  1191. goto idle_irq;
  1192. }
  1193. /* check altstatus */
  1194. status = ap->ops->sff_check_altstatus(ap);
  1195. if (status & ATA_BUSY)
  1196. goto busy_ata;
  1197. /* check main status, clearing INTRQ */
  1198. status = ap->ops->sff_check_status(ap);
  1199. if (unlikely(status & ATA_BUSY))
  1200. goto busy_ata;
  1201. /* ack bmdma irq events */
  1202. ap->ops->sff_irq_clear(ap);
  1203. ata_sff_hsm_move(ap, qc, status, 0);
  1204. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  1205. qc->tf.protocol == ATAPI_PROT_DMA))
  1206. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  1207. busy_ata:
  1208. return 1; /* irq handled */
  1209. idle_irq:
  1210. ap->stats.idle_irq++;
  1211. #ifdef ATA_IRQ_TRAP
  1212. if ((ap->stats.idle_irq % 1000) == 0) {
  1213. ap->ops->irq_ack(ap, 0); /* debug trap */
  1214. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  1215. return 1;
  1216. }
  1217. #endif
  1218. return 0; /* irq not handled */
  1219. }
  1220. static irqreturn_t bfin_ata_interrupt(int irq, void *dev_instance)
  1221. {
  1222. struct ata_host *host = dev_instance;
  1223. unsigned int i;
  1224. unsigned int handled = 0;
  1225. unsigned long flags;
  1226. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  1227. spin_lock_irqsave(&host->lock, flags);
  1228. for (i = 0; i < host->n_ports; i++) {
  1229. struct ata_port *ap;
  1230. ap = host->ports[i];
  1231. if (ap &&
  1232. !(ap->flags & ATA_FLAG_DISABLED)) {
  1233. struct ata_queued_cmd *qc;
  1234. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1235. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  1236. (qc->flags & ATA_QCFLAG_ACTIVE))
  1237. handled |= bfin_ata_host_intr(ap, qc);
  1238. }
  1239. }
  1240. spin_unlock_irqrestore(&host->lock, flags);
  1241. return IRQ_RETVAL(handled);
  1242. }
  1243. static struct scsi_host_template bfin_sht = {
  1244. ATA_BASE_SHT(DRV_NAME),
  1245. .sg_tablesize = SG_NONE,
  1246. .dma_boundary = ATA_DMA_BOUNDARY,
  1247. };
  1248. static struct ata_port_operations bfin_pata_ops = {
  1249. .inherits = &ata_sff_port_ops,
  1250. .set_piomode = bfin_set_piomode,
  1251. .set_dmamode = bfin_set_dmamode,
  1252. .sff_tf_load = bfin_tf_load,
  1253. .sff_tf_read = bfin_tf_read,
  1254. .sff_exec_command = bfin_exec_command,
  1255. .sff_check_status = bfin_check_status,
  1256. .sff_check_altstatus = bfin_check_altstatus,
  1257. .sff_dev_select = bfin_dev_select,
  1258. .bmdma_setup = bfin_bmdma_setup,
  1259. .bmdma_start = bfin_bmdma_start,
  1260. .bmdma_stop = bfin_bmdma_stop,
  1261. .bmdma_status = bfin_bmdma_status,
  1262. .sff_data_xfer = bfin_data_xfer,
  1263. .qc_prep = ata_noop_qc_prep,
  1264. .freeze = bfin_freeze,
  1265. .thaw = bfin_thaw,
  1266. .softreset = bfin_softreset,
  1267. .postreset = bfin_postreset,
  1268. .sff_irq_clear = bfin_irq_clear,
  1269. .sff_irq_on = bfin_irq_on,
  1270. .port_start = bfin_port_start,
  1271. .port_stop = bfin_port_stop,
  1272. };
  1273. static struct ata_port_info bfin_port_info[] = {
  1274. {
  1275. .flags = ATA_FLAG_SLAVE_POSS
  1276. | ATA_FLAG_MMIO
  1277. | ATA_FLAG_NO_LEGACY,
  1278. .pio_mask = ATA_PIO4,
  1279. .mwdma_mask = 0,
  1280. .udma_mask = 0,
  1281. .port_ops = &bfin_pata_ops,
  1282. },
  1283. };
  1284. /**
  1285. * bfin_reset_controller - initialize BF54x ATAPI controller.
  1286. */
  1287. static int bfin_reset_controller(struct ata_host *host)
  1288. {
  1289. void __iomem *base = (void __iomem *)host->ports[0]->ioaddr.ctl_addr;
  1290. int count;
  1291. unsigned short status;
  1292. /* Disable all ATAPI interrupts */
  1293. ATAPI_SET_INT_MASK(base, 0);
  1294. SSYNC();
  1295. /* Assert the RESET signal 25us*/
  1296. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | DEV_RST);
  1297. udelay(30);
  1298. /* Negate the RESET signal for 2ms*/
  1299. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) & ~DEV_RST);
  1300. msleep(2);
  1301. /* Wait on Busy flag to clear */
  1302. count = 10000000;
  1303. do {
  1304. status = read_atapi_register(base, ATA_REG_STATUS);
  1305. } while (--count && (status & ATA_BUSY));
  1306. /* Enable only ATAPI Device interrupt */
  1307. ATAPI_SET_INT_MASK(base, 1);
  1308. SSYNC();
  1309. return (!count);
  1310. }
  1311. /**
  1312. * atapi_io_port - define atapi peripheral port pins.
  1313. */
  1314. static unsigned short atapi_io_port[] = {
  1315. P_ATAPI_RESET,
  1316. P_ATAPI_DIOR,
  1317. P_ATAPI_DIOW,
  1318. P_ATAPI_CS0,
  1319. P_ATAPI_CS1,
  1320. P_ATAPI_DMACK,
  1321. P_ATAPI_DMARQ,
  1322. P_ATAPI_INTRQ,
  1323. P_ATAPI_IORDY,
  1324. P_ATAPI_D0A,
  1325. P_ATAPI_D1A,
  1326. P_ATAPI_D2A,
  1327. P_ATAPI_D3A,
  1328. P_ATAPI_D4A,
  1329. P_ATAPI_D5A,
  1330. P_ATAPI_D6A,
  1331. P_ATAPI_D7A,
  1332. P_ATAPI_D8A,
  1333. P_ATAPI_D9A,
  1334. P_ATAPI_D10A,
  1335. P_ATAPI_D11A,
  1336. P_ATAPI_D12A,
  1337. P_ATAPI_D13A,
  1338. P_ATAPI_D14A,
  1339. P_ATAPI_D15A,
  1340. P_ATAPI_A0A,
  1341. P_ATAPI_A1A,
  1342. P_ATAPI_A2A,
  1343. 0
  1344. };
  1345. /**
  1346. * bfin_atapi_probe - attach a bfin atapi interface
  1347. * @pdev: platform device
  1348. *
  1349. * Register a bfin atapi interface.
  1350. *
  1351. *
  1352. * Platform devices are expected to contain 2 resources per port:
  1353. *
  1354. * - I/O Base (IORESOURCE_IO)
  1355. * - IRQ (IORESOURCE_IRQ)
  1356. *
  1357. */
  1358. static int __devinit bfin_atapi_probe(struct platform_device *pdev)
  1359. {
  1360. int board_idx = 0;
  1361. struct resource *res;
  1362. struct ata_host *host;
  1363. unsigned int fsclk = get_sclk();
  1364. int udma_mode = 5;
  1365. const struct ata_port_info *ppi[] =
  1366. { &bfin_port_info[board_idx], NULL };
  1367. /*
  1368. * Simple resource validation ..
  1369. */
  1370. if (unlikely(pdev->num_resources != 2)) {
  1371. dev_err(&pdev->dev, "invalid number of resources\n");
  1372. return -EINVAL;
  1373. }
  1374. /*
  1375. * Get the register base first
  1376. */
  1377. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1378. if (res == NULL)
  1379. return -EINVAL;
  1380. while (bfin_port_info[board_idx].udma_mask > 0 &&
  1381. udma_fsclk[udma_mode] > fsclk) {
  1382. udma_mode--;
  1383. bfin_port_info[board_idx].udma_mask >>= 1;
  1384. }
  1385. /*
  1386. * Now that that's out of the way, wire up the port..
  1387. */
  1388. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 1);
  1389. if (!host)
  1390. return -ENOMEM;
  1391. host->ports[0]->ioaddr.ctl_addr = (void *)res->start;
  1392. if (peripheral_request_list(atapi_io_port, "atapi-io-port")) {
  1393. dev_err(&pdev->dev, "Requesting Peripherals faild\n");
  1394. return -EFAULT;
  1395. }
  1396. if (bfin_reset_controller(host)) {
  1397. peripheral_free_list(atapi_io_port);
  1398. dev_err(&pdev->dev, "Fail to reset ATAPI device\n");
  1399. return -EFAULT;
  1400. }
  1401. if (ata_host_activate(host, platform_get_irq(pdev, 0),
  1402. bfin_ata_interrupt, IRQF_SHARED, &bfin_sht) != 0) {
  1403. peripheral_free_list(atapi_io_port);
  1404. dev_err(&pdev->dev, "Fail to attach ATAPI device\n");
  1405. return -ENODEV;
  1406. }
  1407. dev_set_drvdata(&pdev->dev, host);
  1408. return 0;
  1409. }
  1410. /**
  1411. * bfin_atapi_remove - unplug a bfin atapi interface
  1412. * @pdev: platform device
  1413. *
  1414. * A bfin atapi device has been unplugged. Perform the needed
  1415. * cleanup. Also called on module unload for any active devices.
  1416. */
  1417. static int __devexit bfin_atapi_remove(struct platform_device *pdev)
  1418. {
  1419. struct device *dev = &pdev->dev;
  1420. struct ata_host *host = dev_get_drvdata(dev);
  1421. ata_host_detach(host);
  1422. dev_set_drvdata(&pdev->dev, NULL);
  1423. peripheral_free_list(atapi_io_port);
  1424. return 0;
  1425. }
  1426. #ifdef CONFIG_PM
  1427. static int bfin_atapi_suspend(struct platform_device *pdev, pm_message_t state)
  1428. {
  1429. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1430. if (host)
  1431. return ata_host_suspend(host, state);
  1432. else
  1433. return 0;
  1434. }
  1435. static int bfin_atapi_resume(struct platform_device *pdev)
  1436. {
  1437. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1438. int ret;
  1439. if (host) {
  1440. ret = bfin_reset_controller(host);
  1441. if (ret) {
  1442. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  1443. return ret;
  1444. }
  1445. ata_host_resume(host);
  1446. }
  1447. return 0;
  1448. }
  1449. #else
  1450. #define bfin_atapi_suspend NULL
  1451. #define bfin_atapi_resume NULL
  1452. #endif
  1453. static struct platform_driver bfin_atapi_driver = {
  1454. .probe = bfin_atapi_probe,
  1455. .remove = __devexit_p(bfin_atapi_remove),
  1456. .suspend = bfin_atapi_suspend,
  1457. .resume = bfin_atapi_resume,
  1458. .driver = {
  1459. .name = DRV_NAME,
  1460. .owner = THIS_MODULE,
  1461. },
  1462. };
  1463. #define ATAPI_MODE_SIZE 10
  1464. static char bfin_atapi_mode[ATAPI_MODE_SIZE];
  1465. static int __init bfin_atapi_init(void)
  1466. {
  1467. pr_info("register bfin atapi driver\n");
  1468. switch(bfin_atapi_mode[0]) {
  1469. case 'p':
  1470. case 'P':
  1471. break;
  1472. case 'm':
  1473. case 'M':
  1474. bfin_port_info[0].mwdma_mask = ATA_MWDMA2;
  1475. break;
  1476. default:
  1477. bfin_port_info[0].udma_mask = ATA_UDMA5;
  1478. };
  1479. return platform_driver_register(&bfin_atapi_driver);
  1480. }
  1481. static void __exit bfin_atapi_exit(void)
  1482. {
  1483. platform_driver_unregister(&bfin_atapi_driver);
  1484. }
  1485. module_init(bfin_atapi_init);
  1486. module_exit(bfin_atapi_exit);
  1487. /*
  1488. * ATAPI mode:
  1489. * pio/PIO
  1490. * udma/UDMA (default)
  1491. * mwdma/MWDMA
  1492. */
  1493. module_param_string(bfin_atapi_mode, bfin_atapi_mode, ATAPI_MODE_SIZE, 0);
  1494. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  1495. MODULE_DESCRIPTION("PATA driver for blackfin 54x ATAPI controller");
  1496. MODULE_LICENSE("GPL");
  1497. MODULE_VERSION(DRV_VERSION);
  1498. MODULE_ALIAS("platform:" DRV_NAME);