pata_bfin.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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/portmux.h>
  17. #include <asm/mach-common/bits/pata.h>
  18. #include <ata.h>
  19. #include <libata.h>
  20. #include "pata_bfin.h"
  21. static struct ata_port port[CONFIG_SYS_SATA_MAX_DEVICE];
  22. /**
  23. * PIO Mode - Frequency compatibility
  24. */
  25. /* mode: 0 1 2 3 4 */
  26. static const u32 pio_fsclk[] =
  27. { 33333333, 33333333, 33333333, 33333333, 33333333 };
  28. /**
  29. * MDMA Mode - Frequency compatibility
  30. */
  31. /* mode: 0 1 2 */
  32. static const u32 mdma_fsclk[] = { 33333333, 33333333, 33333333 };
  33. /**
  34. * UDMA Mode - Frequency compatibility
  35. *
  36. * UDMA5 - 100 MB/s - SCLK = 133 MHz
  37. * UDMA4 - 66 MB/s - SCLK >= 80 MHz
  38. * UDMA3 - 44.4 MB/s - SCLK >= 50 MHz
  39. * UDMA2 - 33 MB/s - SCLK >= 40 MHz
  40. */
  41. /* mode: 0 1 2 3 4 5 */
  42. static const u32 udma_fsclk[] =
  43. { 33333333, 33333333, 40000000, 50000000, 80000000, 133333333 };
  44. /**
  45. * Register transfer timing table
  46. */
  47. /* mode: 0 1 2 3 4 */
  48. /* Cycle Time */
  49. static const u32 reg_t0min[] = { 600, 383, 330, 180, 120 };
  50. /* DIOR/DIOW to end cycle */
  51. static const u32 reg_t2min[] = { 290, 290, 290, 70, 25 };
  52. /* DIOR/DIOW asserted pulse width */
  53. static const u32 reg_teocmin[] = { 290, 290, 290, 80, 70 };
  54. /**
  55. * PIO timing table
  56. */
  57. /* mode: 0 1 2 3 4 */
  58. /* Cycle Time */
  59. static const u32 pio_t0min[] = { 600, 383, 240, 180, 120 };
  60. /* Address valid to DIOR/DIORW */
  61. static const u32 pio_t1min[] = { 70, 50, 30, 30, 25 };
  62. /* DIOR/DIOW to end cycle */
  63. static const u32 pio_t2min[] = { 165, 125, 100, 80, 70 };
  64. /* DIOR/DIOW asserted pulse width */
  65. static const u32 pio_teocmin[] = { 165, 125, 100, 70, 25 };
  66. /* DIOW data hold */
  67. static const u32 pio_t4min[] = { 30, 20, 15, 10, 10 };
  68. /* ******************************************************************
  69. * Multiword DMA timing table
  70. * ******************************************************************
  71. */
  72. /* mode: 0 1 2 */
  73. /* Cycle Time */
  74. static const u32 mdma_t0min[] = { 480, 150, 120 };
  75. /* DIOR/DIOW asserted pulse width */
  76. static const u32 mdma_tdmin[] = { 215, 80, 70 };
  77. /* DMACK to read data released */
  78. static const u32 mdma_thmin[] = { 20, 15, 10 };
  79. /* DIOR/DIOW to DMACK hold */
  80. static const u32 mdma_tjmin[] = { 20, 5, 5 };
  81. /* DIOR negated pulse width */
  82. static const u32 mdma_tkrmin[] = { 50, 50, 25 };
  83. /* DIOR negated pulse width */
  84. static const u32 mdma_tkwmin[] = { 215, 50, 25 };
  85. /* CS[1:0] valid to DIOR/DIOW */
  86. static const u32 mdma_tmmin[] = { 50, 30, 25 };
  87. /* DMACK to read data released */
  88. static const u32 mdma_tzmax[] = { 20, 25, 25 };
  89. /**
  90. * Ultra DMA timing table
  91. */
  92. /* mode: 0 1 2 3 4 5 */
  93. static const u32 udma_tcycmin[] = { 112, 73, 54, 39, 25, 17 };
  94. static const u32 udma_tdvsmin[] = { 70, 48, 31, 20, 7, 5 };
  95. static const u32 udma_tenvmax[] = { 70, 70, 70, 55, 55, 50 };
  96. static const u32 udma_trpmin[] = { 160, 125, 100, 100, 100, 85 };
  97. static const u32 udma_tmin[] = { 5, 5, 5, 5, 3, 3 };
  98. static const u32 udma_tmlimin = 20;
  99. static const u32 udma_tzahmin = 20;
  100. static const u32 udma_tenvmin = 20;
  101. static const u32 udma_tackmin = 20;
  102. static const u32 udma_tssmin = 50;
  103. static void msleep(int count)
  104. {
  105. int i;
  106. for (i = 0; i < count; i++)
  107. udelay(1000);
  108. }
  109. /**
  110. *
  111. * Function: num_clocks_min
  112. *
  113. * Description:
  114. * calculate number of SCLK cycles to meet minimum timing
  115. */
  116. static unsigned short num_clocks_min(unsigned long tmin,
  117. unsigned long fsclk)
  118. {
  119. unsigned long tmp ;
  120. unsigned short result;
  121. tmp = tmin * (fsclk/1000/1000) / 1000;
  122. result = (unsigned short)tmp;
  123. if ((tmp*1000*1000) < (tmin*(fsclk/1000)))
  124. result++;
  125. return result;
  126. }
  127. /**
  128. * bfin_set_piomode - Initialize host controller PATA PIO timings
  129. * @ap: Port whose timings we are configuring
  130. * @pio_mode: mode
  131. *
  132. * Set PIO mode for device.
  133. *
  134. * LOCKING:
  135. * None (inherited from caller).
  136. */
  137. static void bfin_set_piomode(struct ata_port *ap, int pio_mode)
  138. {
  139. int mode = pio_mode - XFER_PIO_0;
  140. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  141. unsigned int fsclk = get_sclk();
  142. unsigned short teoc_reg, t2_reg, teoc_pio;
  143. unsigned short t4_reg, t2_pio, t1_reg;
  144. unsigned short n0, n6, t6min = 5;
  145. /* the most restrictive timing value is t6 and tc, the DIOW - data hold
  146. * If one SCLK pulse is longer than this minimum value then register
  147. * transfers cannot be supported at this frequency.
  148. */
  149. n6 = num_clocks_min(t6min, fsclk);
  150. if (mode >= 0 && mode <= 4 && n6 >= 1) {
  151. debug("set piomode: mode=%d, fsclk=%ud\n", mode, fsclk);
  152. /* calculate the timing values for register transfers. */
  153. while (mode > 0 && pio_fsclk[mode] > fsclk)
  154. mode--;
  155. /* DIOR/DIOW to end cycle time */
  156. t2_reg = num_clocks_min(reg_t2min[mode], fsclk);
  157. /* DIOR/DIOW asserted pulse width */
  158. teoc_reg = num_clocks_min(reg_teocmin[mode], fsclk);
  159. /* Cycle Time */
  160. n0 = num_clocks_min(reg_t0min[mode], fsclk);
  161. /* increase t2 until we meed the minimum cycle length */
  162. if (t2_reg + teoc_reg < n0)
  163. t2_reg = n0 - teoc_reg;
  164. /* calculate the timing values for pio transfers. */
  165. /* DIOR/DIOW to end cycle time */
  166. t2_pio = num_clocks_min(pio_t2min[mode], fsclk);
  167. /* DIOR/DIOW asserted pulse width */
  168. teoc_pio = num_clocks_min(pio_teocmin[mode], fsclk);
  169. /* Cycle Time */
  170. n0 = num_clocks_min(pio_t0min[mode], fsclk);
  171. /* increase t2 until we meed the minimum cycle length */
  172. if (t2_pio + teoc_pio < n0)
  173. t2_pio = n0 - teoc_pio;
  174. /* Address valid to DIOR/DIORW */
  175. t1_reg = num_clocks_min(pio_t1min[mode], fsclk);
  176. /* DIOW data hold */
  177. t4_reg = num_clocks_min(pio_t4min[mode], fsclk);
  178. ATAPI_SET_REG_TIM_0(base, (teoc_reg<<8 | t2_reg));
  179. ATAPI_SET_PIO_TIM_0(base, (t4_reg<<12 | t2_pio<<4 | t1_reg));
  180. ATAPI_SET_PIO_TIM_1(base, teoc_pio);
  181. if (mode > 2) {
  182. ATAPI_SET_CONTROL(base,
  183. ATAPI_GET_CONTROL(base) | IORDY_EN);
  184. } else {
  185. ATAPI_SET_CONTROL(base,
  186. ATAPI_GET_CONTROL(base) & ~IORDY_EN);
  187. }
  188. /* Disable host ATAPI PIO interrupts */
  189. ATAPI_SET_INT_MASK(base, ATAPI_GET_INT_MASK(base)
  190. & ~(PIO_DONE_MASK | HOST_TERM_XFER_MASK));
  191. SSYNC();
  192. }
  193. }
  194. /**
  195. *
  196. * Function: wait_complete
  197. *
  198. * Description: Waits the interrupt from device
  199. *
  200. */
  201. static inline void wait_complete(void __iomem *base, unsigned short mask)
  202. {
  203. unsigned short status;
  204. unsigned int i = 0;
  205. for (i = 0; i < PATA_BFIN_WAIT_TIMEOUT; i++) {
  206. status = ATAPI_GET_INT_STATUS(base) & mask;
  207. if (status)
  208. break;
  209. }
  210. ATAPI_SET_INT_STATUS(base, mask);
  211. }
  212. /**
  213. *
  214. * Function: write_atapi_register
  215. *
  216. * Description: Writes to ATA Device Resgister
  217. *
  218. */
  219. static void write_atapi_register(void __iomem *base,
  220. unsigned long ata_reg, unsigned short value)
  221. {
  222. /* Program the ATA_DEV_TXBUF register with write data (to be
  223. * written into the device).
  224. */
  225. ATAPI_SET_DEV_TXBUF(base, value);
  226. /* Program the ATA_DEV_ADDR register with address of the
  227. * device register (0x01 to 0x0F).
  228. */
  229. ATAPI_SET_DEV_ADDR(base, ata_reg);
  230. /* Program the ATA_CTRL register with dir set to write (1)
  231. */
  232. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  233. /* ensure PIO DMA is not set */
  234. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  235. /* and start the transfer */
  236. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  237. /* Wait for the interrupt to indicate the end of the transfer.
  238. * (We need to wait on and clear rhe ATA_DEV_INT interrupt status)
  239. */
  240. wait_complete(base, PIO_DONE_INT);
  241. }
  242. /**
  243. *
  244. * Function: read_atapi_register
  245. *
  246. *Description: Reads from ATA Device Resgister
  247. *
  248. */
  249. static unsigned short read_atapi_register(void __iomem *base,
  250. unsigned long ata_reg)
  251. {
  252. /* Program the ATA_DEV_ADDR register with address of the
  253. * device register (0x01 to 0x0F).
  254. */
  255. ATAPI_SET_DEV_ADDR(base, ata_reg);
  256. /* Program the ATA_CTRL register with dir set to read (0) and
  257. */
  258. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  259. /* ensure PIO DMA is not set */
  260. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  261. /* and start the transfer */
  262. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  263. /* Wait for the interrupt to indicate the end of the transfer.
  264. * (PIO_DONE interrupt is set and it doesn't seem to matter
  265. * that we don't clear it)
  266. */
  267. wait_complete(base, PIO_DONE_INT);
  268. /* Read the ATA_DEV_RXBUF register with write data (to be
  269. * written into the device).
  270. */
  271. return ATAPI_GET_DEV_RXBUF(base);
  272. }
  273. /**
  274. *
  275. * Function: write_atapi_register_data
  276. *
  277. * Description: Writes to ATA Device Resgister
  278. *
  279. */
  280. static void write_atapi_data(void __iomem *base,
  281. int len, unsigned short *buf)
  282. {
  283. int i;
  284. /* Set transfer length to 1 */
  285. ATAPI_SET_XFER_LEN(base, 1);
  286. /* Program the ATA_DEV_ADDR register with address of the
  287. * ATA_REG_DATA
  288. */
  289. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  290. /* Program the ATA_CTRL register with dir set to write (1)
  291. */
  292. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
  293. /* ensure PIO DMA is not set */
  294. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  295. for (i = 0; i < len; i++) {
  296. /* Program the ATA_DEV_TXBUF register with write data (to be
  297. * written into the device).
  298. */
  299. ATAPI_SET_DEV_TXBUF(base, buf[i]);
  300. /* and start the transfer */
  301. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  302. /* Wait for the interrupt to indicate the end of the transfer.
  303. * (We need to wait on and clear rhe ATA_DEV_INT
  304. * interrupt status)
  305. */
  306. wait_complete(base, PIO_DONE_INT);
  307. }
  308. }
  309. /**
  310. *
  311. * Function: read_atapi_register_data
  312. *
  313. * Description: Reads from ATA Device Resgister
  314. *
  315. */
  316. static void read_atapi_data(void __iomem *base,
  317. int len, unsigned short *buf)
  318. {
  319. int i;
  320. /* Set transfer length to 1 */
  321. ATAPI_SET_XFER_LEN(base, 1);
  322. /* Program the ATA_DEV_ADDR register with address of the
  323. * ATA_REG_DATA
  324. */
  325. ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
  326. /* Program the ATA_CTRL register with dir set to read (0) and
  327. */
  328. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
  329. /* ensure PIO DMA is not set */
  330. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
  331. for (i = 0; i < len; i++) {
  332. /* and start the transfer */
  333. ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
  334. /* Wait for the interrupt to indicate the end of the transfer.
  335. * (PIO_DONE interrupt is set and it doesn't seem to matter
  336. * that we don't clear it)
  337. */
  338. wait_complete(base, PIO_DONE_INT);
  339. /* Read the ATA_DEV_RXBUF register with write data (to be
  340. * written into the device).
  341. */
  342. buf[i] = ATAPI_GET_DEV_RXBUF(base);
  343. }
  344. }
  345. /**
  346. * bfin_check_status - Read device status reg & clear interrupt
  347. * @ap: port where the device is
  348. *
  349. * Note: Original code is ata_check_status().
  350. */
  351. static u8 bfin_check_status(struct ata_port *ap)
  352. {
  353. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  354. return read_atapi_register(base, ATA_REG_STATUS);
  355. }
  356. /**
  357. * bfin_check_altstatus - Read device alternate status reg
  358. * @ap: port where the device is
  359. */
  360. static u8 bfin_check_altstatus(struct ata_port *ap)
  361. {
  362. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  363. return read_atapi_register(base, ATA_REG_ALTSTATUS);
  364. }
  365. /**
  366. * bfin_ata_busy_wait - Wait for a port status register
  367. * @ap: Port to wait for.
  368. * @bits: bits that must be clear
  369. * @max: number of 10uS waits to perform
  370. *
  371. * Waits up to max*10 microseconds for the selected bits in the port's
  372. * status register to be cleared.
  373. * Returns final value of status register.
  374. *
  375. * LOCKING:
  376. * Inherited from caller.
  377. */
  378. static inline u8 bfin_ata_busy_wait(struct ata_port *ap, unsigned int bits,
  379. unsigned int max, u8 usealtstatus)
  380. {
  381. u8 status;
  382. do {
  383. udelay(10);
  384. if (usealtstatus)
  385. status = bfin_check_altstatus(ap);
  386. else
  387. status = bfin_check_status(ap);
  388. max--;
  389. } while (status != 0xff && (status & bits) && (max > 0));
  390. return status;
  391. }
  392. /**
  393. * bfin_ata_busy_sleep - sleep until BSY clears, or timeout
  394. * @ap: port containing status register to be polled
  395. * @tmout_pat: impatience timeout in msecs
  396. * @tmout: overall timeout in msecs
  397. *
  398. * Sleep until ATA Status register bit BSY clears,
  399. * or a timeout occurs.
  400. *
  401. * RETURNS:
  402. * 0 on success, -errno otherwise.
  403. */
  404. static int bfin_ata_busy_sleep(struct ata_port *ap,
  405. long tmout_pat, unsigned long tmout)
  406. {
  407. u8 status;
  408. status = bfin_ata_busy_wait(ap, ATA_BUSY, 300, 0);
  409. while (status != 0xff && (status & ATA_BUSY) && tmout_pat > 0) {
  410. msleep(50);
  411. tmout_pat -= 50;
  412. status = bfin_ata_busy_wait(ap, ATA_BUSY, 3, 0);
  413. }
  414. if (status != 0xff && (status & ATA_BUSY))
  415. printf("port is slow to respond, please be patient "
  416. "(Status 0x%x)\n", status);
  417. while (status != 0xff && (status & ATA_BUSY) && tmout_pat > 0) {
  418. msleep(50);
  419. tmout_pat -= 50;
  420. status = bfin_check_status(ap);
  421. }
  422. if (status == 0xff)
  423. return -ENODEV;
  424. if (status & ATA_BUSY) {
  425. printf("port failed to respond "
  426. "(%lu secs, Status 0x%x)\n",
  427. DIV_ROUND_UP(tmout, 1000), status);
  428. return -EBUSY;
  429. }
  430. return 0;
  431. }
  432. /**
  433. * bfin_dev_select - Select device 0/1 on ATA bus
  434. * @ap: ATA channel to manipulate
  435. * @device: ATA device (numbered from zero) to select
  436. *
  437. * Note: Original code is ata_sff_dev_select().
  438. */
  439. static void bfin_dev_select(struct ata_port *ap, unsigned int device)
  440. {
  441. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  442. u8 tmp;
  443. if (device == 0)
  444. tmp = ATA_DEVICE_OBS;
  445. else
  446. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  447. write_atapi_register(base, ATA_REG_DEVICE, tmp);
  448. udelay(1);
  449. }
  450. /**
  451. * bfin_devchk - PATA device presence detection
  452. * @ap: ATA channel to examine
  453. * @device: Device to examine (starting at zero)
  454. *
  455. * Note: Original code is ata_devchk().
  456. */
  457. static unsigned int bfin_devchk(struct ata_port *ap,
  458. unsigned int device)
  459. {
  460. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  461. u8 nsect, lbal;
  462. bfin_dev_select(ap, device);
  463. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  464. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  465. write_atapi_register(base, ATA_REG_NSECT, 0xaa);
  466. write_atapi_register(base, ATA_REG_LBAL, 0x55);
  467. write_atapi_register(base, ATA_REG_NSECT, 0x55);
  468. write_atapi_register(base, ATA_REG_LBAL, 0xaa);
  469. nsect = read_atapi_register(base, ATA_REG_NSECT);
  470. lbal = read_atapi_register(base, ATA_REG_LBAL);
  471. if ((nsect == 0x55) && (lbal == 0xaa))
  472. return 1; /* we found a device */
  473. return 0; /* nothing found */
  474. }
  475. /**
  476. * bfin_bus_post_reset - PATA device post reset
  477. *
  478. * Note: Original code is ata_bus_post_reset().
  479. */
  480. static void bfin_bus_post_reset(struct ata_port *ap, unsigned int devmask)
  481. {
  482. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  483. unsigned int dev0 = devmask & (1 << 0);
  484. unsigned int dev1 = devmask & (1 << 1);
  485. long deadline;
  486. /* if device 0 was found in ata_devchk, wait for its
  487. * BSY bit to clear
  488. */
  489. if (dev0)
  490. bfin_ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  491. /* if device 1 was found in ata_devchk, wait for
  492. * register access, then wait for BSY to clear
  493. */
  494. deadline = ATA_TMOUT_BOOT;
  495. while (dev1) {
  496. u8 nsect, lbal;
  497. bfin_dev_select(ap, 1);
  498. nsect = read_atapi_register(base, ATA_REG_NSECT);
  499. lbal = read_atapi_register(base, ATA_REG_LBAL);
  500. if ((nsect == 1) && (lbal == 1))
  501. break;
  502. if (deadline <= 0) {
  503. dev1 = 0;
  504. break;
  505. }
  506. msleep(50); /* give drive a breather */
  507. deadline -= 50;
  508. }
  509. if (dev1)
  510. bfin_ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
  511. /* is all this really necessary? */
  512. bfin_dev_select(ap, 0);
  513. if (dev1)
  514. bfin_dev_select(ap, 1);
  515. if (dev0)
  516. bfin_dev_select(ap, 0);
  517. }
  518. /**
  519. * bfin_bus_softreset - PATA device software reset
  520. *
  521. * Note: Original code is ata_bus_softreset().
  522. */
  523. static unsigned int bfin_bus_softreset(struct ata_port *ap,
  524. unsigned int devmask)
  525. {
  526. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  527. /* software reset. causes dev0 to be selected */
  528. write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg);
  529. udelay(20);
  530. write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg | ATA_SRST);
  531. udelay(20);
  532. write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg);
  533. /* spec mandates ">= 2ms" before checking status.
  534. * We wait 150ms, because that was the magic delay used for
  535. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  536. * between when the ATA command register is written, and then
  537. * status is checked. Because waiting for "a while" before
  538. * checking status is fine, post SRST, we perform this magic
  539. * delay here as well.
  540. *
  541. * Old drivers/ide uses the 2mS rule and then waits for ready
  542. */
  543. msleep(150);
  544. /* Before we perform post reset processing we want to see if
  545. * the bus shows 0xFF because the odd clown forgets the D7
  546. * pulldown resistor.
  547. */
  548. if (bfin_check_status(ap) == 0xFF)
  549. return 0;
  550. bfin_bus_post_reset(ap, devmask);
  551. return 0;
  552. }
  553. /**
  554. * bfin_softreset - reset host port via ATA SRST
  555. * @ap: port to reset
  556. *
  557. * Note: Original code is ata_sff_softreset().
  558. */
  559. static int bfin_softreset(struct ata_port *ap)
  560. {
  561. unsigned int err_mask;
  562. ap->dev_mask = 0;
  563. /* determine if device 0/1 are present.
  564. * only one device is supported on one port by now.
  565. */
  566. if (bfin_devchk(ap, 0))
  567. ap->dev_mask |= (1 << 0);
  568. else if (bfin_devchk(ap, 1))
  569. ap->dev_mask |= (1 << 1);
  570. else
  571. return -ENODEV;
  572. /* select device 0 again */
  573. bfin_dev_select(ap, 0);
  574. /* issue bus reset */
  575. err_mask = bfin_bus_softreset(ap, ap->dev_mask);
  576. if (err_mask) {
  577. printf("SRST failed (err_mask=0x%x)\n",
  578. err_mask);
  579. ap->dev_mask = 0;
  580. return -EIO;
  581. }
  582. return 0;
  583. }
  584. /**
  585. * bfin_irq_clear - Clear ATAPI interrupt.
  586. * @ap: Port associated with this ATA transaction.
  587. *
  588. * Note: Original code is ata_sff_irq_clear().
  589. */
  590. static void bfin_irq_clear(struct ata_port *ap)
  591. {
  592. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  593. ATAPI_SET_INT_STATUS(base, ATAPI_GET_INT_STATUS(base)|ATAPI_DEV_INT
  594. | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
  595. | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT);
  596. }
  597. static u8 bfin_wait_for_irq(struct ata_port *ap, unsigned int max)
  598. {
  599. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  600. do {
  601. if (ATAPI_GET_INT_STATUS(base) & (ATAPI_DEV_INT
  602. | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
  603. | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT)) {
  604. break;
  605. }
  606. udelay(1000);
  607. max--;
  608. } while ((max > 0));
  609. return max == 0;
  610. }
  611. /**
  612. * bfin_ata_reset_port - initialize BFIN ATAPI port.
  613. */
  614. static int bfin_ata_reset_port(struct ata_port *ap)
  615. {
  616. void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
  617. int count;
  618. unsigned short status;
  619. /* Disable all ATAPI interrupts */
  620. ATAPI_SET_INT_MASK(base, 0);
  621. SSYNC();
  622. /* Assert the RESET signal 25us*/
  623. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | DEV_RST);
  624. udelay(30);
  625. /* Negate the RESET signal for 2ms*/
  626. ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) & ~DEV_RST);
  627. msleep(2);
  628. /* Wait on Busy flag to clear */
  629. count = 10000000;
  630. do {
  631. status = read_atapi_register(base, ATA_REG_STATUS);
  632. } while (--count && (status & ATA_BUSY));
  633. /* Enable only ATAPI Device interrupt */
  634. ATAPI_SET_INT_MASK(base, 1);
  635. SSYNC();
  636. return !count;
  637. }
  638. /**
  639. *
  640. * Function: bfin_config_atapi_gpio
  641. *
  642. * Description: Configures the ATAPI pins for use
  643. *
  644. */
  645. static int bfin_config_atapi_gpio(struct ata_port *ap)
  646. {
  647. const unsigned short pins[] = {
  648. P_ATAPI_RESET, P_ATAPI_DIOR, P_ATAPI_DIOW, P_ATAPI_CS0,
  649. P_ATAPI_CS1, P_ATAPI_DMACK, P_ATAPI_DMARQ, P_ATAPI_INTRQ,
  650. P_ATAPI_IORDY, P_ATAPI_D0A, P_ATAPI_D1A, P_ATAPI_D2A,
  651. P_ATAPI_D3A, P_ATAPI_D4A, P_ATAPI_D5A, P_ATAPI_D6A,
  652. P_ATAPI_D7A, P_ATAPI_D8A, P_ATAPI_D9A, P_ATAPI_D10A,
  653. P_ATAPI_D11A, P_ATAPI_D12A, P_ATAPI_D13A, P_ATAPI_D14A,
  654. P_ATAPI_D15A, P_ATAPI_A0A, P_ATAPI_A1A, P_ATAPI_A2A, 0,
  655. };
  656. peripheral_request_list(pins, "pata_bfin");
  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. }