pata_bfin.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * Driver for Blackfin on-chip ATAPI controller.
  3. *
  4. * Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright (c) 2008 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <config.h>
  13. #include <asm/byteorder.h>
  14. #include <asm/io.h>
  15. #include <asm/errno.h>
  16. #include <asm/mach-common/bits/pata.h>
  17. #include <ata.h>
  18. #include <libata.h>
  19. #include "pata_bfin.h"
  20. static struct ata_port port[CONFIG_SYS_SATA_MAX_DEVICE];
  21. /**
  22. * PIO Mode - Frequency compatibility
  23. */
  24. /* mode: 0 1 2 3 4 */
  25. static const u32 pio_fsclk[] =
  26. { 33333333, 33333333, 33333333, 33333333, 33333333 };
  27. /**
  28. * MDMA Mode - Frequency compatibility
  29. */
  30. /* mode: 0 1 2 */
  31. static const u32 mdma_fsclk[] = { 33333333, 33333333, 33333333 };
  32. /**
  33. * UDMA Mode - Frequency compatibility
  34. *
  35. * UDMA5 - 100 MB/s - SCLK = 133 MHz
  36. * UDMA4 - 66 MB/s - SCLK >= 80 MHz
  37. * UDMA3 - 44.4 MB/s - SCLK >= 50 MHz
  38. * UDMA2 - 33 MB/s - SCLK >= 40 MHz
  39. */
  40. /* mode: 0 1 2 3 4 5 */
  41. static const u32 udma_fsclk[] =
  42. { 33333333, 33333333, 40000000, 50000000, 80000000, 133333333 };
  43. /**
  44. * Register transfer timing table
  45. */
  46. /* mode: 0 1 2 3 4 */
  47. /* Cycle Time */
  48. static const u32 reg_t0min[] = { 600, 383, 330, 180, 120 };
  49. /* DIOR/DIOW to end cycle */
  50. static const u32 reg_t2min[] = { 290, 290, 290, 70, 25 };
  51. /* DIOR/DIOW asserted pulse width */
  52. static const u32 reg_teocmin[] = { 290, 290, 290, 80, 70 };
  53. /**
  54. * PIO timing table
  55. */
  56. /* mode: 0 1 2 3 4 */
  57. /* Cycle Time */
  58. static const u32 pio_t0min[] = { 600, 383, 240, 180, 120 };
  59. /* Address valid to DIOR/DIORW */
  60. static const u32 pio_t1min[] = { 70, 50, 30, 30, 25 };
  61. /* DIOR/DIOW to end cycle */
  62. static const u32 pio_t2min[] = { 165, 125, 100, 80, 70 };
  63. /* DIOR/DIOW asserted pulse width */
  64. static const u32 pio_teocmin[] = { 165, 125, 100, 70, 25 };
  65. /* DIOW data hold */
  66. static const u32 pio_t4min[] = { 30, 20, 15, 10, 10 };
  67. /* ******************************************************************
  68. * Multiword DMA timing table
  69. * ******************************************************************
  70. */
  71. /* mode: 0 1 2 */
  72. /* Cycle Time */
  73. static const u32 mdma_t0min[] = { 480, 150, 120 };
  74. /* DIOR/DIOW asserted pulse width */
  75. static const u32 mdma_tdmin[] = { 215, 80, 70 };
  76. /* DMACK to read data released */
  77. static const u32 mdma_thmin[] = { 20, 15, 10 };
  78. /* DIOR/DIOW to DMACK hold */
  79. static const u32 mdma_tjmin[] = { 20, 5, 5 };
  80. /* DIOR negated pulse width */
  81. static const u32 mdma_tkrmin[] = { 50, 50, 25 };
  82. /* DIOR negated pulse width */
  83. static const u32 mdma_tkwmin[] = { 215, 50, 25 };
  84. /* CS[1:0] valid to DIOR/DIOW */
  85. static const u32 mdma_tmmin[] = { 50, 30, 25 };
  86. /* DMACK to read data released */
  87. static const u32 mdma_tzmax[] = { 20, 25, 25 };
  88. /**
  89. * Ultra DMA timing table
  90. */
  91. /* mode: 0 1 2 3 4 5 */
  92. static const u32 udma_tcycmin[] = { 112, 73, 54, 39, 25, 17 };
  93. static const u32 udma_tdvsmin[] = { 70, 48, 31, 20, 7, 5 };
  94. static const u32 udma_tenvmax[] = { 70, 70, 70, 55, 55, 50 };
  95. static const u32 udma_trpmin[] = { 160, 125, 100, 100, 100, 85 };
  96. static const u32 udma_tmin[] = { 5, 5, 5, 5, 3, 3 };
  97. static const u32 udma_tmlimin = 20;
  98. static const u32 udma_tzahmin = 20;
  99. static const u32 udma_tenvmin = 20;
  100. static const u32 udma_tackmin = 20;
  101. static const u32 udma_tssmin = 50;
  102. static void msleep(int count)
  103. {
  104. int i;
  105. for (i = 0; i < count; i++)
  106. udelay(1000);
  107. }
  108. /**
  109. *
  110. * Function: num_clocks_min
  111. *
  112. * Description:
  113. * calculate number of SCLK cycles to meet minimum timing
  114. */
  115. static unsigned short num_clocks_min(unsigned long tmin,
  116. unsigned long fsclk)
  117. {
  118. unsigned long tmp ;
  119. unsigned short result;
  120. tmp = tmin * (fsclk/1000/1000) / 1000;
  121. result = (unsigned short)tmp;
  122. if ((tmp*1000*1000) < (tmin*(fsclk/1000)))
  123. result++;
  124. return result;
  125. }
  126. /**
  127. * bfin_set_piomode - Initialize host controller PATA PIO timings
  128. * @ap: Port whose timings we are configuring
  129. * @pio_mode: mode
  130. *
  131. * Set PIO mode for device.
  132. *
  133. * LOCKING:
  134. * None (inherited from caller).
  135. */
  136. static void bfin_set_piomode(struct ata_port *ap, int pio_mode)
  137. {
  138. int mode = pio_mode - XFER_PIO_0;
  139. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  140. unsigned int fsclk = get_sclk();
  141. unsigned short teoc_reg, t2_reg, teoc_pio;
  142. unsigned short t4_reg, t2_pio, t1_reg;
  143. unsigned short n0, n6, t6min = 5;
  144. /* the most restrictive timing value is t6 and tc, the DIOW - data hold
  145. * If one SCLK pulse is longer than this minimum value then register
  146. * transfers cannot be supported at this frequency.
  147. */
  148. n6 = num_clocks_min(t6min, fsclk);
  149. if (mode >= 0 && mode <= 4 && n6 >= 1) {
  150. debug("set piomode: mode=%d, fsclk=%ud\n", mode, fsclk);
  151. /* calculate the timing values for register transfers. */
  152. while (mode > 0 && pio_fsclk[mode] > fsclk)
  153. mode--;
  154. /* DIOR/DIOW to end cycle time */
  155. t2_reg = num_clocks_min(reg_t2min[mode], fsclk);
  156. /* DIOR/DIOW asserted pulse width */
  157. teoc_reg = num_clocks_min(reg_teocmin[mode], fsclk);
  158. /* Cycle Time */
  159. n0 = num_clocks_min(reg_t0min[mode], fsclk);
  160. /* increase t2 until we meed the minimum cycle length */
  161. if (t2_reg + teoc_reg < n0)
  162. t2_reg = n0 - teoc_reg;
  163. /* calculate the timing values for pio transfers. */
  164. /* DIOR/DIOW to end cycle time */
  165. t2_pio = num_clocks_min(pio_t2min[mode], fsclk);
  166. /* DIOR/DIOW asserted pulse width */
  167. teoc_pio = num_clocks_min(pio_teocmin[mode], fsclk);
  168. /* Cycle Time */
  169. n0 = num_clocks_min(pio_t0min[mode], fsclk);
  170. /* increase t2 until we meed the minimum cycle length */
  171. if (t2_pio + teoc_pio < n0)
  172. t2_pio = n0 - teoc_pio;
  173. /* Address valid to DIOR/DIORW */
  174. t1_reg = num_clocks_min(pio_t1min[mode], fsclk);
  175. /* DIOW data hold */
  176. t4_reg = num_clocks_min(pio_t4min[mode], fsclk);
  177. ATAPI_SET_REG_TIM_0(base, (teoc_reg<<8 | t2_reg));
  178. ATAPI_SET_PIO_TIM_0(base, (t4_reg<<12 | t2_pio<<4 | t1_reg));
  179. ATAPI_SET_PIO_TIM_1(base, teoc_pio);
  180. if (mode > 2) {
  181. ATAPI_SET_CONTROL(base,
  182. ATAPI_GET_CONTROL(base) | IORDY_EN);
  183. } else {
  184. ATAPI_SET_CONTROL(base,
  185. ATAPI_GET_CONTROL(base) & ~IORDY_EN);
  186. }
  187. /* Disable host ATAPI PIO interrupts */
  188. ATAPI_SET_INT_MASK(base, ATAPI_GET_INT_MASK(base)
  189. & ~(PIO_DONE_MASK | HOST_TERM_XFER_MASK));
  190. SSYNC();
  191. }
  192. }
  193. /**
  194. *
  195. * Function: wait_complete
  196. *
  197. * Description: Waits the interrupt from device
  198. *
  199. */
  200. static inline void wait_complete(void __iomem *base, unsigned short mask)
  201. {
  202. unsigned short status;
  203. unsigned int i = 0;
  204. for (i = 0; i < PATA_BFIN_WAIT_TIMEOUT; i++) {
  205. status = ATAPI_GET_INT_STATUS(base) & mask;
  206. if (status)
  207. break;
  208. }
  209. ATAPI_SET_INT_STATUS(base, mask);
  210. }
  211. /**
  212. *
  213. * Function: write_atapi_register
  214. *
  215. * Description: Writes to ATA Device Resgister
  216. *
  217. */
  218. static void write_atapi_register(void __iomem *base,
  219. unsigned long ata_reg, unsigned short value)
  220. {
  221. /* Program the ATA_DEV_TXBUF register with write data (to be
  222. * written into the device).
  223. */
  224. ATAPI_SET_DEV_TXBUF(base, value);
  225. /* Program the ATA_DEV_ADDR register with address of the
  226. * device register (0x01 to 0x0F).
  227. */
  228. ATAPI_SET_DEV_ADDR(base, ata_reg);
  229. /* Program the ATA_CTRL register with dir set to write (1)
  230. */
  231. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  232. /* ensure PIO DMA is not set */
  233. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  234. /* and start the transfer */
  235. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  236. /* Wait for the interrupt to indicate the end of the transfer.
  237. * (We need to wait on and clear rhe ATA_DEV_INT interrupt status)
  238. */
  239. wait_complete(base, PIO_DONE_INT);
  240. }
  241. /**
  242. *
  243. * Function: read_atapi_register
  244. *
  245. *Description: Reads from ATA Device Resgister
  246. *
  247. */
  248. static unsigned short read_atapi_register(void __iomem *base,
  249. unsigned long ata_reg)
  250. {
  251. /* Program the ATA_DEV_ADDR register with address of the
  252. * device register (0x01 to 0x0F).
  253. */
  254. ATAPI_SET_DEV_ADDR(base, ata_reg);
  255. /* Program the ATA_CTRL register with dir set to read (0) and
  256. */
  257. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  258. /* ensure PIO DMA is not set */
  259. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  260. /* and start the transfer */
  261. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  262. /* Wait for the interrupt to indicate the end of the transfer.
  263. * (PIO_DONE interrupt is set and it doesn't seem to matter
  264. * that we don't clear it)
  265. */
  266. wait_complete(base, PIO_DONE_INT);
  267. /* Read the ATA_DEV_RXBUF register with write data (to be
  268. * written into the device).
  269. */
  270. return ATAPI_GET_DEV_RXBUF(base);
  271. }
  272. /**
  273. *
  274. * Function: write_atapi_register_data
  275. *
  276. * Description: Writes to ATA Device Resgister
  277. *
  278. */
  279. static void write_atapi_data(void __iomem *base,
  280. int len, unsigned short *buf)
  281. {
  282. int i;
  283. /* Set transfer length to 1 */
  284. ATAPI_SET_XFER_LEN(base, 1);
  285. /* Program the ATA_DEV_ADDR register with address of the
  286. * ATA_REG_DATA
  287. */
  288. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  289. /* Program the ATA_CTRL register with dir set to write (1)
  290. */
  291. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  292. /* ensure PIO DMA is not set */
  293. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  294. for (i = 0; i < len; i++) {
  295. /* Program the ATA_DEV_TXBUF register with write data (to be
  296. * written into the device).
  297. */
  298. ATAPI_SET_DEV_TXBUF(base, buf[i]);
  299. /* and start the transfer */
  300. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  301. /* Wait for the interrupt to indicate the end of the transfer.
  302. * (We need to wait on and clear rhe ATA_DEV_INT
  303. * interrupt status)
  304. */
  305. wait_complete(base, PIO_DONE_INT);
  306. }
  307. }
  308. /**
  309. *
  310. * Function: read_atapi_register_data
  311. *
  312. * Description: Reads from ATA Device Resgister
  313. *
  314. */
  315. static void read_atapi_data(void __iomem *base,
  316. int len, unsigned short *buf)
  317. {
  318. int i;
  319. /* Set transfer length to 1 */
  320. ATAPI_SET_XFER_LEN(base, 1);
  321. /* Program the ATA_DEV_ADDR register with address of the
  322. * ATA_REG_DATA
  323. */
  324. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  325. /* Program the ATA_CTRL register with dir set to read (0) and
  326. */
  327. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  328. /* ensure PIO DMA is not set */
  329. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  330. for (i = 0; i < len; i++) {
  331. /* and start the transfer */
  332. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  333. /* Wait for the interrupt to indicate the end of the transfer.
  334. * (PIO_DONE interrupt is set and it doesn't seem to matter
  335. * that we don't clear it)
  336. */
  337. wait_complete(base, PIO_DONE_INT);
  338. /* Read the ATA_DEV_RXBUF register with write data (to be
  339. * written into the device).
  340. */
  341. buf[i] = ATAPI_GET_DEV_RXBUF(base);
  342. }
  343. }
  344. /**
  345. * bfin_check_status - Read device status reg & clear interrupt
  346. * @ap: port where the device is
  347. *
  348. * Note: Original code is ata_check_status().
  349. */
  350. static u8 bfin_check_status(struct ata_port *ap)
  351. {
  352. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  353. return read_atapi_register(base, ATA_REG_STATUS);
  354. }
  355. /**
  356. * bfin_check_altstatus - Read device alternate status reg
  357. * @ap: port where the device is
  358. */
  359. static u8 bfin_check_altstatus(struct ata_port *ap)
  360. {
  361. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  362. return read_atapi_register(base, ATA_REG_ALTSTATUS);
  363. }
  364. /**
  365. * bfin_ata_busy_wait - Wait for a port status register
  366. * @ap: Port to wait for.
  367. * @bits: bits that must be clear
  368. * @max: number of 10uS waits to perform
  369. *
  370. * Waits up to max*10 microseconds for the selected bits in the port's
  371. * status register to be cleared.
  372. * Returns final value of status register.
  373. *
  374. * LOCKING:
  375. * Inherited from caller.
  376. */
  377. static inline u8 bfin_ata_busy_wait(struct ata_port *ap, unsigned int bits,
  378. unsigned int max, u8 usealtstatus)
  379. {
  380. u8 status;
  381. do {
  382. udelay(10);
  383. if (usealtstatus)
  384. status = bfin_check_altstatus(ap);
  385. else
  386. status = bfin_check_status(ap);
  387. max--;
  388. } while (status != 0xff && (status & bits) && (max > 0));
  389. return status;
  390. }
  391. /**
  392. * bfin_ata_busy_sleep - sleep until BSY clears, or timeout
  393. * @ap: port containing status register to be polled
  394. * @tmout_pat: impatience timeout in msecs
  395. * @tmout: overall timeout in msecs
  396. *
  397. * Sleep until ATA Status register bit BSY clears,
  398. * or a timeout occurs.
  399. *
  400. * RETURNS:
  401. * 0 on success, -errno otherwise.
  402. */
  403. static int bfin_ata_busy_sleep(struct ata_port *ap,
  404. long tmout_pat, unsigned long tmout)
  405. {
  406. u8 status;
  407. status = bfin_ata_busy_wait(ap, ATA_BUSY, 300, 0);
  408. while (status != 0xff && (status & ATA_BUSY) && tmout_pat > 0) {
  409. msleep(50);
  410. tmout_pat -= 50;
  411. status = bfin_ata_busy_wait(ap, ATA_BUSY, 3, 0);
  412. }
  413. if (status != 0xff && (status & ATA_BUSY))
  414. printf("port is slow to respond, please be patient "
  415. "(Status 0x%x)\n", status);
  416. while (status != 0xff && (status & ATA_BUSY) && tmout_pat > 0) {
  417. msleep(50);
  418. tmout_pat -= 50;
  419. status = bfin_check_status(ap);
  420. }
  421. if (status == 0xff)
  422. return -ENODEV;
  423. if (status & ATA_BUSY) {
  424. printf("port failed to respond "
  425. "(%lu secs, Status 0x%x)\n",
  426. DIV_ROUND_UP(tmout, 1000), status);
  427. return -EBUSY;
  428. }
  429. return 0;
  430. }
  431. /**
  432. * bfin_dev_select - Select device 0/1 on ATA bus
  433. * @ap: ATA channel to manipulate
  434. * @device: ATA device (numbered from zero) to select
  435. *
  436. * Note: Original code is ata_sff_dev_select().
  437. */
  438. static void bfin_dev_select(struct ata_port *ap, unsigned int device)
  439. {
  440. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  441. u8 tmp;
  442. if (device == 0)
  443. tmp = ATA_DEVICE_OBS;
  444. else
  445. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  446. write_atapi_register(base, ATA_REG_DEVICE, tmp);
  447. udelay(1);
  448. }
  449. /**
  450. * bfin_devchk - PATA device presence detection
  451. * @ap: ATA channel to examine
  452. * @device: Device to examine (starting at zero)
  453. *
  454. * Note: Original code is ata_devchk().
  455. */
  456. static unsigned int bfin_devchk(struct ata_port *ap,
  457. unsigned int device)
  458. {
  459. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  460. u8 nsect, lbal;
  461. bfin_dev_select(ap, device);
  462. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  463. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  464. write_atapi_register(base, ATA_REG_NSECT, 0xaa);
  465. write_atapi_register(base, ATA_REG_LBAL, 0x55);
  466. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  467. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  468. nsect = read_atapi_register(base, ATA_REG_NSECT);
  469. lbal = read_atapi_register(base, ATA_REG_LBAL);
  470. if ((nsect == 0x55) && (lbal == 0xaa))
  471. return 1; /* we found a device */
  472. return 0; /* nothing found */
  473. }
  474. /**
  475. * bfin_bus_post_reset - PATA device post reset
  476. *
  477. * Note: Original code is ata_bus_post_reset().
  478. */
  479. static void bfin_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  480. {
  481. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  482. unsigned int dev0 = devmask & (1 << 0);
  483. unsigned int dev1 = devmask & (1 << 1);
  484. long deadline;
  485. /* if device 0 was found in ata_devchk, wait for its
  486. * BSY bit to clear
  487. */
  488. if (dev0)
  489. bfin_ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  490. /* if device 1 was found in ata_devchk, wait for
  491. * register access, then wait for BSY to clear
  492. */
  493. deadline = ATA_TMOUT_BOOT;
  494. while (dev1) {
  495. u8 nsect, lbal;
  496. bfin_dev_select(ap, 1);
  497. nsect = read_atapi_register(base, ATA_REG_NSECT);
  498. lbal = read_atapi_register(base, ATA_REG_LBAL);
  499. if ((nsect == 1) && (lbal == 1))
  500. break;
  501. if (deadline <= 0) {
  502. dev1 = 0;
  503. break;
  504. }
  505. msleep(50); /* give drive a breather */
  506. deadline -= 50;
  507. }
  508. if (dev1)
  509. bfin_ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  510. /* is all this really necessary? */
  511. bfin_dev_select(ap, 0);
  512. if (dev1)
  513. bfin_dev_select(ap, 1);
  514. if (dev0)
  515. bfin_dev_select(ap, 0);
  516. }
  517. /**
  518. * bfin_bus_softreset - PATA device software reset
  519. *
  520. * Note: Original code is ata_bus_softreset().
  521. */
  522. static unsigned int bfin_bus_softreset(struct ata_port *ap,
  523. unsigned int devmask)
  524. {
  525. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  526. /* software reset. causes dev0 to be selected */
  527. write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg);
  528. udelay(20);
  529. write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg | ATA_SRST);
  530. udelay(20);
  531. write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg);
  532. /* spec mandates ">= 2ms" before checking status.
  533. * We wait 150ms, because that was the magic delay used for
  534. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  535. * between when the ATA command register is written, and then
  536. * status is checked. Because waiting for "a while" before
  537. * checking status is fine, post SRST, we perform this magic
  538. * delay here as well.
  539. *
  540. * Old drivers/ide uses the 2mS rule and then waits for ready
  541. */
  542. msleep(150);
  543. /* Before we perform post reset processing we want to see if
  544. * the bus shows 0xFF because the odd clown forgets the D7
  545. * pulldown resistor.
  546. */
  547. if (bfin_check_status(ap) == 0xFF)
  548. return 0;
  549. bfin_bus_post_reset(ap, devmask);
  550. return 0;
  551. }
  552. /**
  553. * bfin_softreset - reset host port via ATA SRST
  554. * @ap: port to reset
  555. *
  556. * Note: Original code is ata_sff_softreset().
  557. */
  558. static int bfin_softreset(struct ata_port *ap)
  559. {
  560. unsigned int err_mask;
  561. ap->dev_mask = 0;
  562. /* determine if device 0/1 are present.
  563. * only one device is supported on one port by now.
  564. */
  565. if (bfin_devchk(ap, 0))
  566. ap->dev_mask |= (1 << 0);
  567. else if (bfin_devchk(ap, 1))
  568. ap->dev_mask |= (1 << 1);
  569. else
  570. return -ENODEV;
  571. /* select device 0 again */
  572. bfin_dev_select(ap, 0);
  573. /* issue bus reset */
  574. err_mask = bfin_bus_softreset(ap, ap->dev_mask);
  575. if (err_mask) {
  576. printf("SRST failed (err_mask=0x%x)\n",
  577. err_mask);
  578. ap->dev_mask = 0;
  579. return -EIO;
  580. }
  581. return 0;
  582. }
  583. /**
  584. * bfin_irq_clear - Clear ATAPI interrupt.
  585. * @ap: Port associated with this ATA transaction.
  586. *
  587. * Note: Original code is ata_sff_irq_clear().
  588. */
  589. static void bfin_irq_clear(struct ata_port *ap)
  590. {
  591. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  592. ATAPI_SET_INT_STATUS(base, ATAPI_GET_INT_STATUS(base)|ATAPI_DEV_INT
  593. | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
  594. | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT);
  595. }
  596. static u8 bfin_wait_for_irq(struct ata_port *ap, unsigned int max)
  597. {
  598. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  599. do {
  600. if (ATAPI_GET_INT_STATUS(base) & (ATAPI_DEV_INT
  601. | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
  602. | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT)) {
  603. break;
  604. }
  605. udelay(1000);
  606. max--;
  607. } while ((max > 0));
  608. return max == 0;
  609. }
  610. /**
  611. * bfin_ata_reset_port - initialize BFIN ATAPI port.
  612. */
  613. static int bfin_ata_reset_port(struct ata_port *ap)
  614. {
  615. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  616. int count;
  617. unsigned short status;
  618. /* Disable all ATAPI interrupts */
  619. ATAPI_SET_INT_MASK(base, 0);
  620. SSYNC();
  621. /* Assert the RESET signal 25us*/
  622. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | DEV_RST);
  623. udelay(30);
  624. /* Negate the RESET signal for 2ms*/
  625. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) & ~DEV_RST);
  626. msleep(2);
  627. /* Wait on Busy flag to clear */
  628. count = 10000000;
  629. do {
  630. status = read_atapi_register(base, ATA_REG_STATUS);
  631. } while (--count && (status & ATA_BUSY));
  632. /* Enable only ATAPI Device interrupt */
  633. ATAPI_SET_INT_MASK(base, 1);
  634. SSYNC();
  635. return !count;
  636. }
  637. /**
  638. *
  639. * Function: bfin_config_atapi_gpio
  640. *
  641. * Description: Configures the ATAPI pins for use
  642. *
  643. */
  644. static int bfin_config_atapi_gpio(struct ata_port *ap)
  645. {
  646. bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0x4);
  647. bfin_write_PORTH_MUX(bfin_read_PORTH_MUX() & ~0x30);
  648. bfin_write_PORTH_DIR_SET(0x4);
  649. bfin_write_PORTJ_FER(0x7f8);
  650. bfin_write_PORTJ_MUX(bfin_read_PORTI_MUX() & ~0x3fffc0);
  651. bfin_write_PORTJ_DIR_SET(0x5f8);
  652. bfin_write_PORTJ_DIR_CLEAR(0x200);
  653. bfin_write_PORTJ_INEN(0x200);
  654. bfin_write_PINT2_ASSIGN(0x0707);
  655. bfin_write_PINT2_MASK_SET(0x200);
  656. SSYNC();
  657. return 0;
  658. }
  659. /**
  660. * bfin_atapi_probe - attach a bfin atapi interface
  661. * @pdev: platform device
  662. *
  663. * Register a bfin atapi interface.
  664. *
  665. *
  666. * Platform devices are expected to contain 2 resources per port:
  667. *
  668. * - I/O Base (IORESOURCE_IO)
  669. * - IRQ (IORESOURCE_IRQ)
  670. *
  671. */
  672. static int bfin_ata_probe_port(struct ata_port *ap)
  673. {
  674. if (bfin_config_atapi_gpio(ap)) {
  675. printf("Requesting Peripherals faild\n");
  676. return -EFAULT;
  677. }
  678. if (bfin_ata_reset_port(ap)) {
  679. printf("Fail to reset ATAPI device\n");
  680. return -EFAULT;
  681. }
  682. if (ap->ata_mode >= XFER_PIO_0 && ap->ata_mode <= XFER_PIO_4)
  683. bfin_set_piomode(ap, ap->ata_mode);
  684. else {
  685. printf("Given ATA data transfer mode is not supported.\n");
  686. return -EFAULT;
  687. }
  688. return 0;
  689. }
  690. #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
  691. static void bfin_ata_identify(struct ata_port *ap, int dev)
  692. {
  693. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  694. u8 status = 0;
  695. static u16 iobuf[ATA_SECTOR_WORDS];
  696. u64 n_sectors = 0;
  697. hd_driveid_t *iop = (hd_driveid_t *)iobuf;
  698. memset(iobuf, 0, sizeof(iobuf));
  699. if (!(ap->dev_mask & (1 << dev)))
  700. return;
  701. debug("port=%d dev=%d\n", ap->port_no, dev);
  702. bfin_dev_select(ap, dev);
  703. status = 0;
  704. /* Device Identify Command */
  705. write_atapi_register(base, ATA_REG_CMD, ATA_CMD_ID_ATA);
  706. bfin_check_altstatus(ap);
  707. udelay(10);
  708. status = bfin_ata_busy_wait(ap, ATA_BUSY, 1000, 0);
  709. if (status & ATA_ERR) {
  710. printf("\ndevice not responding\n");
  711. ap->dev_mask &= ~(1 << dev);
  712. return;
  713. }
  714. read_atapi_data(base, ATA_SECTOR_WORDS, iobuf);
  715. ata_swap_buf_le16(iobuf, ATA_SECTOR_WORDS);
  716. /* we require LBA and DMA support (bits 8 & 9 of word 49) */
  717. if (!ata_id_has_dma(iobuf) || !ata_id_has_lba(iobuf))
  718. printf("ata%u: no dma/lba\n", ap->port_no);
  719. #ifdef DEBUG
  720. ata_dump_id(iobuf);
  721. #endif
  722. n_sectors = ata_id_n_sectors(iobuf);
  723. if (n_sectors == 0) {
  724. ap->dev_mask &= ~(1 << dev);
  725. return;
  726. }
  727. ata_id_c_string(iobuf, (unsigned char *)sata_dev_desc[ap->port_no].revision,
  728. ATA_ID_FW_REV, sizeof(sata_dev_desc[ap->port_no].revision));
  729. ata_id_c_string(iobuf, (unsigned char *)sata_dev_desc[ap->port_no].vendor,
  730. ATA_ID_PROD, sizeof(sata_dev_desc[ap->port_no].vendor));
  731. ata_id_c_string(iobuf, (unsigned char *)sata_dev_desc[ap->port_no].product,
  732. ATA_ID_SERNO, sizeof(sata_dev_desc[ap->port_no].product));
  733. if ((iop->config & 0x0080) == 0x0080)
  734. sata_dev_desc[ap->port_no].removable = 1;
  735. else
  736. sata_dev_desc[ap->port_no].removable = 0;
  737. sata_dev_desc[ap->port_no].lba = (u32) n_sectors;
  738. debug("lba=0x%x\n", sata_dev_desc[ap->port_no].lba);
  739. #ifdef CONFIG_LBA48
  740. if (iop->command_set_2 & 0x0400)
  741. sata_dev_desc[ap->port_no].lba48 = 1;
  742. else
  743. sata_dev_desc[ap->port_no].lba48 = 0;
  744. #endif
  745. /* assuming HD */
  746. sata_dev_desc[ap->port_no].type = DEV_TYPE_HARDDISK;
  747. sata_dev_desc[ap->port_no].blksz = ATA_SECT_SIZE;
  748. sata_dev_desc[ap->port_no].lun = 0; /* just to fill something in... */
  749. printf("PATA device#%d %s is found on ata port#%d.\n",
  750. ap->port_no%PATA_DEV_NUM_PER_PORT,
  751. sata_dev_desc[ap->port_no].vendor,
  752. ap->port_no/PATA_DEV_NUM_PER_PORT);
  753. }
  754. static void bfin_ata_set_Feature_cmd(struct ata_port *ap, int dev)
  755. {
  756. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  757. u8 status = 0;
  758. if (!(ap->dev_mask & (1 << dev)))
  759. return;
  760. bfin_dev_select(ap, dev);
  761. write_atapi_register(base, ATA_REG_FEATURE, SETFEATURES_XFER);
  762. write_atapi_register(base, ATA_REG_NSECT, ap->ata_mode);
  763. write_atapi_register(base, ATA_REG_LBAL, 0);
  764. write_atapi_register(base, ATA_REG_LBAM, 0);
  765. write_atapi_register(base, ATA_REG_LBAH, 0);
  766. write_atapi_register(base, ATA_REG_DEVICE, ATA_DEVICE_OBS);
  767. write_atapi_register(base, ATA_REG_CMD, ATA_CMD_SET_FEATURES);
  768. udelay(50);
  769. msleep(150);
  770. status = bfin_ata_busy_wait(ap, ATA_BUSY, 5000, 0);
  771. if ((status & (ATA_BUSY | ATA_ERR))) {
  772. printf("Error : status 0x%02x\n", status);
  773. ap->dev_mask &= ~(1 << dev);
  774. }
  775. }
  776. int scan_sata(int dev)
  777. {
  778. /* dev is the index of each ata device in the system. one PATA port
  779. * contains 2 devices. one element in scan_done array indicates one
  780. * PATA port. device connected to one PATA port is selected by
  781. * bfin_dev_select() before access.
  782. */
  783. struct ata_port *ap = &port[dev];
  784. static int scan_done[(CONFIG_SYS_SATA_MAX_DEVICE+1)/PATA_DEV_NUM_PER_PORT];
  785. if (scan_done[dev/PATA_DEV_NUM_PER_PORT])
  786. return 0;
  787. /* Check for attached device */
  788. if (!bfin_ata_probe_port(ap)) {
  789. if (bfin_softreset(ap)) {
  790. /* soft reset failed, try a hard one */
  791. bfin_ata_reset_port(ap);
  792. if (bfin_softreset(ap))
  793. scan_done[dev/PATA_DEV_NUM_PER_PORT] = 1;
  794. } else {
  795. scan_done[dev/PATA_DEV_NUM_PER_PORT] = 1;
  796. }
  797. }
  798. if (scan_done[dev/PATA_DEV_NUM_PER_PORT]) {
  799. /* Probe device and set xfer mode */
  800. bfin_ata_identify(ap, dev%PATA_DEV_NUM_PER_PORT);
  801. bfin_ata_set_Feature_cmd(ap, dev%PATA_DEV_NUM_PER_PORT);
  802. init_part(&sata_dev_desc[dev]);
  803. return 0;
  804. }
  805. printf("PATA device#%d is not present on ATA port#%d.\n",
  806. ap->port_no%PATA_DEV_NUM_PER_PORT,
  807. ap->port_no/PATA_DEV_NUM_PER_PORT);
  808. return -1;
  809. }
  810. int init_sata(int dev)
  811. {
  812. struct ata_port *ap = &port[dev];
  813. static u8 init_done;
  814. int res = 1;
  815. if (init_done)
  816. return res;
  817. init_done = 1;
  818. switch (dev/PATA_DEV_NUM_PER_PORT) {
  819. case 0:
  820. ap->ioaddr.ctl_addr = ATAPI_CONTROL;
  821. ap->ata_mode = CONFIG_BFIN_ATA_MODE;
  822. break;
  823. default:
  824. printf("Tried to scan unknown port %d.\n", dev);
  825. return res;
  826. }
  827. if (ap->ata_mode < XFER_PIO_0 || ap->ata_mode > XFER_PIO_4) {
  828. ap->ata_mode = XFER_PIO_4;
  829. printf("DMA mode is not supported. Set to PIO mode 4.\n");
  830. }
  831. ap->port_no = dev;
  832. ap->ctl_reg = 0x8; /*Default value of control reg */
  833. res = 0;
  834. return res;
  835. }
  836. /* Read up to 255 sectors
  837. *
  838. * Returns sectors read
  839. */
  840. static u8 do_one_read(struct ata_port *ap, u64 blknr, u8 blkcnt, u16 *buffer,
  841. uchar lba48)
  842. {
  843. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  844. u8 sr = 0;
  845. u8 status;
  846. u16 err = 0;
  847. if (!(bfin_check_status(ap) & ATA_DRDY)) {
  848. printf("Device ata%d not ready\n", ap->port_no);
  849. return 0;
  850. }
  851. /* Set up transfer */
  852. #ifdef CONFIG_LBA48
  853. if (lba48) {
  854. /* write high bits */
  855. write_atapi_register(base, ATA_REG_NSECT, 0);
  856. write_atapi_register(base, ATA_REG_LBAL, (blknr >> 24) & 0xFF);
  857. write_atapi_register(base, ATA_REG_LBAM, (blknr >> 32) & 0xFF);
  858. write_atapi_register(base, ATA_REG_LBAH, (blknr >> 40) & 0xFF);
  859. }
  860. #endif
  861. write_atapi_register(base, ATA_REG_NSECT, blkcnt);
  862. write_atapi_register(base, ATA_REG_LBAL, (blknr >> 0) & 0xFF);
  863. write_atapi_register(base, ATA_REG_LBAM, (blknr >> 8) & 0xFF);
  864. write_atapi_register(base, ATA_REG_LBAH, (blknr >> 16) & 0xFF);
  865. #ifdef CONFIG_LBA48
  866. if (lba48) {
  867. write_atapi_register(base, ATA_REG_DEVICE, ATA_LBA);
  868. write_atapi_register(base, ATA_REG_CMD, ATA_CMD_PIO_READ_EXT);
  869. } else
  870. #endif
  871. {
  872. write_atapi_register(base, ATA_REG_DEVICE, ATA_LBA | ((blknr >> 24) & 0xF));
  873. write_atapi_register(base, ATA_REG_CMD, ATA_CMD_PIO_READ);
  874. }
  875. status = bfin_ata_busy_wait(ap, ATA_BUSY, 500000, 1);
  876. if (status & (ATA_BUSY | ATA_ERR)) {
  877. printf("Device %d not responding status 0x%x.\n", ap->port_no, status);
  878. err = read_atapi_register(base, ATA_REG_ERR);
  879. printf("Error reg = 0x%x\n", err);
  880. return sr;
  881. }
  882. while (blkcnt--) {
  883. if (bfin_wait_for_irq(ap, 500)) {
  884. printf("ata%u irq failed\n", ap->port_no);
  885. return sr;
  886. }
  887. status = bfin_check_status(ap);
  888. if (status & ATA_ERR) {
  889. err = read_atapi_register(base, ATA_REG_ERR);
  890. printf("ata%u error %d\n", ap->port_no, err);
  891. return sr;
  892. }
  893. bfin_irq_clear(ap);
  894. /* Read one sector */
  895. read_atapi_data(base, ATA_SECTOR_WORDS, buffer);
  896. buffer += ATA_SECTOR_WORDS;
  897. sr++;
  898. }
  899. return sr;
  900. }
  901. ulong sata_read(int dev, ulong block, ulong blkcnt, void *buff)
  902. {
  903. struct ata_port *ap = &port[dev];
  904. ulong n = 0, sread;
  905. u16 *buffer = (u16 *) buff;
  906. u8 status = 0;
  907. u64 blknr = (u64) block;
  908. unsigned char lba48 = 0;
  909. #ifdef CONFIG_LBA48
  910. if (blknr > 0xfffffff) {
  911. if (!sata_dev_desc[dev].lba48) {
  912. printf("Drive doesn't support 48-bit addressing\n");
  913. return 0;
  914. }
  915. /* more than 28 bits used, use 48bit mode */
  916. lba48 = 1;
  917. }
  918. #endif
  919. bfin_dev_select(ap, dev%PATA_DEV_NUM_PER_PORT);
  920. while (blkcnt > 0) {
  921. if (blkcnt > 255)
  922. sread = 255;
  923. else
  924. sread = blkcnt;
  925. status = do_one_read(ap, blknr, sread, buffer, lba48);
  926. if (status != sread) {
  927. printf("Read failed\n");
  928. return n;
  929. }
  930. blkcnt -= sread;
  931. blknr += sread;
  932. n += sread;
  933. buffer += sread * ATA_SECTOR_WORDS;
  934. }
  935. return n;
  936. }
  937. ulong sata_write(int dev, ulong block, ulong blkcnt, const void *buff)
  938. {
  939. struct ata_port *ap = &port[dev];
  940. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  941. ulong n = 0;
  942. u16 *buffer = (u16 *) buff;
  943. unsigned char status = 0;
  944. u64 blknr = (u64) block;
  945. #ifdef CONFIG_LBA48
  946. unsigned char lba48 = 0;
  947. if (blknr > 0xfffffff) {
  948. if (!sata_dev_desc[dev].lba48) {
  949. printf("Drive doesn't support 48-bit addressing\n");
  950. return 0;
  951. }
  952. /* more than 28 bits used, use 48bit mode */
  953. lba48 = 1;
  954. }
  955. #endif
  956. bfin_dev_select(ap, dev%PATA_DEV_NUM_PER_PORT);
  957. while (blkcnt-- > 0) {
  958. status = bfin_ata_busy_wait(ap, ATA_BUSY, 50000, 0);
  959. if (status & ATA_BUSY) {
  960. printf("ata%u failed to respond\n", ap->port_no);
  961. return n;
  962. }
  963. #ifdef CONFIG_LBA48
  964. if (lba48) {
  965. /* write high bits */
  966. write_atapi_register(base, ATA_REG_NSECT, 0);
  967. write_atapi_register(base, ATA_REG_LBAL,
  968. (blknr >> 24) & 0xFF);
  969. write_atapi_register(base, ATA_REG_LBAM,
  970. (blknr >> 32) & 0xFF);
  971. write_atapi_register(base, ATA_REG_LBAH,
  972. (blknr >> 40) & 0xFF);
  973. }
  974. #endif
  975. write_atapi_register(base, ATA_REG_NSECT, 1);
  976. write_atapi_register(base, ATA_REG_LBAL, (blknr >> 0) & 0xFF);
  977. write_atapi_register(base, ATA_REG_LBAM, (blknr >> 8) & 0xFF);
  978. write_atapi_register(base, ATA_REG_LBAH, (blknr >> 16) & 0xFF);
  979. #ifdef CONFIG_LBA48
  980. if (lba48) {
  981. write_atapi_register(base, ATA_REG_DEVICE, ATA_LBA);
  982. write_atapi_register(base, ATA_REG_CMD,
  983. ATA_CMD_PIO_WRITE_EXT);
  984. } else
  985. #endif
  986. {
  987. write_atapi_register(base, ATA_REG_DEVICE,
  988. ATA_LBA | ((blknr >> 24) & 0xF));
  989. write_atapi_register(base, ATA_REG_CMD,
  990. ATA_CMD_PIO_WRITE);
  991. }
  992. /*may take up to 5 sec */
  993. status = bfin_ata_busy_wait(ap, ATA_BUSY, 50000, 0);
  994. if ((status & (ATA_DRQ | ATA_BUSY | ATA_ERR)) != ATA_DRQ) {
  995. printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
  996. ap->port_no, (ulong) blknr, status);
  997. return n;
  998. }
  999. write_atapi_data(base, ATA_SECTOR_WORDS, buffer);
  1000. bfin_check_altstatus(ap);
  1001. udelay(1);
  1002. ++n;
  1003. ++blknr;
  1004. buffer += ATA_SECTOR_WORDS;
  1005. }
  1006. return n;
  1007. }