pata_bfin.c 30 KB

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