pata_bfin.c 30 KB

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