pata_mpc52xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * drivers/ata/pata_mpc52xx.c
  3. *
  4. * libata driver for the Freescale MPC52xx on-chip IDE interface
  5. *
  6. * Copyright (C) 2006 Sylvain Munaut <tnt@246tNt.com>
  7. * Copyright (C) 2003 Mipsys - Benjamin Herrenschmidt
  8. *
  9. * This file is licensed under the terms of the GNU General Public License
  10. * version 2. This program is licensed "as is" without any warranty of any
  11. * kind, whether express or implied.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/libata.h>
  18. #include <linux/of_platform.h>
  19. #include <asm/types.h>
  20. #include <asm/prom.h>
  21. #include <asm/mpc52xx.h>
  22. #define DRV_NAME "mpc52xx_ata"
  23. #define DRV_VERSION "0.1.2"
  24. /* Private structures used by the driver */
  25. struct mpc52xx_ata_timings {
  26. u32 pio1;
  27. u32 pio2;
  28. };
  29. struct mpc52xx_ata_priv {
  30. unsigned int ipb_period;
  31. struct mpc52xx_ata __iomem * ata_regs;
  32. int ata_irq;
  33. struct mpc52xx_ata_timings timings[2];
  34. int csel;
  35. };
  36. /* ATAPI-4 PIO specs (in ns) */
  37. static const int ataspec_t0[5] = {600, 383, 240, 180, 120};
  38. static const int ataspec_t1[5] = { 70, 50, 30, 30, 25};
  39. static const int ataspec_t2_8[5] = {290, 290, 290, 80, 70};
  40. static const int ataspec_t2_16[5] = {165, 125, 100, 80, 70};
  41. static const int ataspec_t2i[5] = { 0, 0, 0, 70, 25};
  42. static const int ataspec_t4[5] = { 30, 20, 15, 10, 10};
  43. static const int ataspec_ta[5] = { 35, 35, 35, 35, 35};
  44. #define CALC_CLKCYC(c,v) ((((v)+(c)-1)/(c)))
  45. /* Bit definitions inside the registers */
  46. #define MPC52xx_ATA_HOSTCONF_SMR 0x80000000UL /* State machine reset */
  47. #define MPC52xx_ATA_HOSTCONF_FR 0x40000000UL /* FIFO Reset */
  48. #define MPC52xx_ATA_HOSTCONF_IE 0x02000000UL /* Enable interrupt in PIO */
  49. #define MPC52xx_ATA_HOSTCONF_IORDY 0x01000000UL /* Drive supports IORDY protocol */
  50. #define MPC52xx_ATA_HOSTSTAT_TIP 0x80000000UL /* Transaction in progress */
  51. #define MPC52xx_ATA_HOSTSTAT_UREP 0x40000000UL /* UDMA Read Extended Pause */
  52. #define MPC52xx_ATA_HOSTSTAT_RERR 0x02000000UL /* Read Error */
  53. #define MPC52xx_ATA_HOSTSTAT_WERR 0x01000000UL /* Write Error */
  54. #define MPC52xx_ATA_FIFOSTAT_EMPTY 0x01 /* FIFO Empty */
  55. #define MPC52xx_ATA_DMAMODE_WRITE 0x01 /* Write DMA */
  56. #define MPC52xx_ATA_DMAMODE_READ 0x02 /* Read DMA */
  57. #define MPC52xx_ATA_DMAMODE_UDMA 0x04 /* UDMA enabled */
  58. #define MPC52xx_ATA_DMAMODE_IE 0x08 /* Enable drive interrupt to CPU in DMA mode */
  59. #define MPC52xx_ATA_DMAMODE_FE 0x10 /* FIFO Flush enable in Rx mode */
  60. #define MPC52xx_ATA_DMAMODE_FR 0x20 /* FIFO Reset */
  61. #define MPC52xx_ATA_DMAMODE_HUT 0x40 /* Host UDMA burst terminate */
  62. /* Structure of the hardware registers */
  63. struct mpc52xx_ata {
  64. /* Host interface registers */
  65. u32 config; /* ATA + 0x00 Host configuration */
  66. u32 host_status; /* ATA + 0x04 Host controller status */
  67. u32 pio1; /* ATA + 0x08 PIO Timing 1 */
  68. u32 pio2; /* ATA + 0x0c PIO Timing 2 */
  69. u32 mdma1; /* ATA + 0x10 MDMA Timing 1 */
  70. u32 mdma2; /* ATA + 0x14 MDMA Timing 2 */
  71. u32 udma1; /* ATA + 0x18 UDMA Timing 1 */
  72. u32 udma2; /* ATA + 0x1c UDMA Timing 2 */
  73. u32 udma3; /* ATA + 0x20 UDMA Timing 3 */
  74. u32 udma4; /* ATA + 0x24 UDMA Timing 4 */
  75. u32 udma5; /* ATA + 0x28 UDMA Timing 5 */
  76. u32 share_cnt; /* ATA + 0x2c ATA share counter */
  77. u32 reserved0[3];
  78. /* FIFO registers */
  79. u32 fifo_data; /* ATA + 0x3c */
  80. u8 fifo_status_frame; /* ATA + 0x40 */
  81. u8 fifo_status; /* ATA + 0x41 */
  82. u16 reserved7[1];
  83. u8 fifo_control; /* ATA + 0x44 */
  84. u8 reserved8[5];
  85. u16 fifo_alarm; /* ATA + 0x4a */
  86. u16 reserved9;
  87. u16 fifo_rdp; /* ATA + 0x4e */
  88. u16 reserved10;
  89. u16 fifo_wrp; /* ATA + 0x52 */
  90. u16 reserved11;
  91. u16 fifo_lfrdp; /* ATA + 0x56 */
  92. u16 reserved12;
  93. u16 fifo_lfwrp; /* ATA + 0x5a */
  94. /* Drive TaskFile registers */
  95. u8 tf_control; /* ATA + 0x5c TASKFILE Control/Alt Status */
  96. u8 reserved13[3];
  97. u16 tf_data; /* ATA + 0x60 TASKFILE Data */
  98. u16 reserved14;
  99. u8 tf_features; /* ATA + 0x64 TASKFILE Features/Error */
  100. u8 reserved15[3];
  101. u8 tf_sec_count; /* ATA + 0x68 TASKFILE Sector Count */
  102. u8 reserved16[3];
  103. u8 tf_sec_num; /* ATA + 0x6c TASKFILE Sector Number */
  104. u8 reserved17[3];
  105. u8 tf_cyl_low; /* ATA + 0x70 TASKFILE Cylinder Low */
  106. u8 reserved18[3];
  107. u8 tf_cyl_high; /* ATA + 0x74 TASKFILE Cylinder High */
  108. u8 reserved19[3];
  109. u8 tf_dev_head; /* ATA + 0x78 TASKFILE Device/Head */
  110. u8 reserved20[3];
  111. u8 tf_command; /* ATA + 0x7c TASKFILE Command/Status */
  112. u8 dma_mode; /* ATA + 0x7d ATA Host DMA Mode configuration */
  113. u8 reserved21[2];
  114. };
  115. /* ======================================================================== */
  116. /* Aux fns */
  117. /* ======================================================================== */
  118. /* MPC52xx low level hw control */
  119. static int
  120. mpc52xx_ata_compute_pio_timings(struct mpc52xx_ata_priv *priv, int dev, int pio)
  121. {
  122. struct mpc52xx_ata_timings *timing = &priv->timings[dev];
  123. unsigned int ipb_period = priv->ipb_period;
  124. unsigned int t0, t1, t2_8, t2_16, t2i, t4, ta;
  125. if ((pio<0) || (pio>4))
  126. return -EINVAL;
  127. t0 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t0[pio]);
  128. t1 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t1[pio]);
  129. t2_8 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2_8[pio]);
  130. t2_16 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2_16[pio]);
  131. t2i = CALC_CLKCYC(ipb_period, 1000 * ataspec_t2i[pio]);
  132. t4 = CALC_CLKCYC(ipb_period, 1000 * ataspec_t4[pio]);
  133. ta = CALC_CLKCYC(ipb_period, 1000 * ataspec_ta[pio]);
  134. timing->pio1 = (t0 << 24) | (t2_8 << 16) | (t2_16 << 8) | (t2i);
  135. timing->pio2 = (t4 << 24) | (t1 << 16) | (ta << 8);
  136. return 0;
  137. }
  138. static void
  139. mpc52xx_ata_apply_timings(struct mpc52xx_ata_priv *priv, int device)
  140. {
  141. struct mpc52xx_ata __iomem *regs = priv->ata_regs;
  142. struct mpc52xx_ata_timings *timing = &priv->timings[device];
  143. out_be32(&regs->pio1, timing->pio1);
  144. out_be32(&regs->pio2, timing->pio2);
  145. out_be32(&regs->mdma1, 0);
  146. out_be32(&regs->mdma2, 0);
  147. out_be32(&regs->udma1, 0);
  148. out_be32(&regs->udma2, 0);
  149. out_be32(&regs->udma3, 0);
  150. out_be32(&regs->udma4, 0);
  151. out_be32(&regs->udma5, 0);
  152. priv->csel = device;
  153. }
  154. static int
  155. mpc52xx_ata_hw_init(struct mpc52xx_ata_priv *priv)
  156. {
  157. struct mpc52xx_ata __iomem *regs = priv->ata_regs;
  158. int tslot;
  159. /* Clear share_cnt (all sample code do this ...) */
  160. out_be32(&regs->share_cnt, 0);
  161. /* Configure and reset host */
  162. out_be32(&regs->config,
  163. MPC52xx_ATA_HOSTCONF_IE |
  164. MPC52xx_ATA_HOSTCONF_IORDY |
  165. MPC52xx_ATA_HOSTCONF_SMR |
  166. MPC52xx_ATA_HOSTCONF_FR);
  167. udelay(10);
  168. out_be32(&regs->config,
  169. MPC52xx_ATA_HOSTCONF_IE |
  170. MPC52xx_ATA_HOSTCONF_IORDY);
  171. /* Set the time slot to 1us */
  172. tslot = CALC_CLKCYC(priv->ipb_period, 1000000);
  173. out_be32(&regs->share_cnt, tslot << 16 );
  174. /* Init timings to PIO0 */
  175. memset(priv->timings, 0x00, 2*sizeof(struct mpc52xx_ata_timings));
  176. mpc52xx_ata_compute_pio_timings(priv, 0, 0);
  177. mpc52xx_ata_compute_pio_timings(priv, 1, 0);
  178. mpc52xx_ata_apply_timings(priv, 0);
  179. return 0;
  180. }
  181. /* ======================================================================== */
  182. /* libata driver */
  183. /* ======================================================================== */
  184. static void
  185. mpc52xx_ata_set_piomode(struct ata_port *ap, struct ata_device *adev)
  186. {
  187. struct mpc52xx_ata_priv *priv = ap->host->private_data;
  188. int pio, rv;
  189. pio = adev->pio_mode - XFER_PIO_0;
  190. rv = mpc52xx_ata_compute_pio_timings(priv, adev->devno, pio);
  191. if (rv) {
  192. printk(KERN_ERR DRV_NAME
  193. ": Trying to select invalid PIO mode %d\n", pio);
  194. return;
  195. }
  196. mpc52xx_ata_apply_timings(priv, adev->devno);
  197. }
  198. static void
  199. mpc52xx_ata_dev_select(struct ata_port *ap, unsigned int device)
  200. {
  201. struct mpc52xx_ata_priv *priv = ap->host->private_data;
  202. if (device != priv->csel)
  203. mpc52xx_ata_apply_timings(priv, device);
  204. ata_sff_dev_select(ap,device);
  205. }
  206. static struct scsi_host_template mpc52xx_ata_sht = {
  207. ATA_PIO_SHT(DRV_NAME),
  208. };
  209. static struct ata_port_operations mpc52xx_ata_port_ops = {
  210. .inherits = &ata_sff_port_ops,
  211. .sff_dev_select = mpc52xx_ata_dev_select,
  212. .cable_detect = ata_cable_40wire,
  213. .set_piomode = mpc52xx_ata_set_piomode,
  214. .post_internal_cmd = ATA_OP_NULL,
  215. };
  216. static int __devinit
  217. mpc52xx_ata_init_one(struct device *dev, struct mpc52xx_ata_priv *priv,
  218. unsigned long raw_ata_regs)
  219. {
  220. struct ata_host *host;
  221. struct ata_port *ap;
  222. struct ata_ioports *aio;
  223. host = ata_host_alloc(dev, 1);
  224. if (!host)
  225. return -ENOMEM;
  226. ap = host->ports[0];
  227. ap->flags |= ATA_FLAG_SLAVE_POSS;
  228. ap->pio_mask = 0x1f; /* Up to PIO4 */
  229. ap->mwdma_mask = 0x00; /* No MWDMA */
  230. ap->udma_mask = 0x00; /* No UDMA */
  231. ap->ops = &mpc52xx_ata_port_ops;
  232. host->private_data = priv;
  233. aio = &ap->ioaddr;
  234. aio->cmd_addr = NULL; /* Don't have a classic reg block */
  235. aio->altstatus_addr = &priv->ata_regs->tf_control;
  236. aio->ctl_addr = &priv->ata_regs->tf_control;
  237. aio->data_addr = &priv->ata_regs->tf_data;
  238. aio->error_addr = &priv->ata_regs->tf_features;
  239. aio->feature_addr = &priv->ata_regs->tf_features;
  240. aio->nsect_addr = &priv->ata_regs->tf_sec_count;
  241. aio->lbal_addr = &priv->ata_regs->tf_sec_num;
  242. aio->lbam_addr = &priv->ata_regs->tf_cyl_low;
  243. aio->lbah_addr = &priv->ata_regs->tf_cyl_high;
  244. aio->device_addr = &priv->ata_regs->tf_dev_head;
  245. aio->status_addr = &priv->ata_regs->tf_command;
  246. aio->command_addr = &priv->ata_regs->tf_command;
  247. ata_port_desc(ap, "ata_regs 0x%lx", raw_ata_regs);
  248. /* activate host */
  249. return ata_host_activate(host, priv->ata_irq, ata_sff_interrupt, 0,
  250. &mpc52xx_ata_sht);
  251. }
  252. static struct mpc52xx_ata_priv *
  253. mpc52xx_ata_remove_one(struct device *dev)
  254. {
  255. struct ata_host *host = dev_get_drvdata(dev);
  256. struct mpc52xx_ata_priv *priv = host->private_data;
  257. ata_host_detach(host);
  258. return priv;
  259. }
  260. /* ======================================================================== */
  261. /* OF Platform driver */
  262. /* ======================================================================== */
  263. static int __devinit
  264. mpc52xx_ata_probe(struct of_device *op, const struct of_device_id *match)
  265. {
  266. unsigned int ipb_freq;
  267. struct resource res_mem;
  268. int ata_irq;
  269. struct mpc52xx_ata __iomem *ata_regs;
  270. struct mpc52xx_ata_priv *priv;
  271. int rv;
  272. /* Get ipb frequency */
  273. ipb_freq = mpc52xx_find_ipb_freq(op->node);
  274. if (!ipb_freq) {
  275. printk(KERN_ERR DRV_NAME ": "
  276. "Unable to find IPB Bus frequency\n" );
  277. return -ENODEV;
  278. }
  279. /* Get IRQ and register */
  280. rv = of_address_to_resource(op->node, 0, &res_mem);
  281. if (rv) {
  282. printk(KERN_ERR DRV_NAME ": "
  283. "Error while parsing device node resource\n" );
  284. return rv;
  285. }
  286. ata_irq = irq_of_parse_and_map(op->node, 0);
  287. if (ata_irq == NO_IRQ) {
  288. printk(KERN_ERR DRV_NAME ": "
  289. "Error while mapping the irq\n");
  290. return -EINVAL;
  291. }
  292. /* Request mem region */
  293. if (!devm_request_mem_region(&op->dev, res_mem.start,
  294. sizeof(struct mpc52xx_ata), DRV_NAME)) {
  295. printk(KERN_ERR DRV_NAME ": "
  296. "Error while requesting mem region\n");
  297. rv = -EBUSY;
  298. goto err;
  299. }
  300. /* Remap registers */
  301. ata_regs = devm_ioremap(&op->dev, res_mem.start,
  302. sizeof(struct mpc52xx_ata));
  303. if (!ata_regs) {
  304. printk(KERN_ERR DRV_NAME ": "
  305. "Error while mapping register set\n");
  306. rv = -ENOMEM;
  307. goto err;
  308. }
  309. /* Prepare our private structure */
  310. priv = devm_kzalloc(&op->dev, sizeof(struct mpc52xx_ata_priv),
  311. GFP_ATOMIC);
  312. if (!priv) {
  313. printk(KERN_ERR DRV_NAME ": "
  314. "Error while allocating private structure\n");
  315. rv = -ENOMEM;
  316. goto err;
  317. }
  318. priv->ipb_period = 1000000000 / (ipb_freq / 1000);
  319. priv->ata_regs = ata_regs;
  320. priv->ata_irq = ata_irq;
  321. priv->csel = -1;
  322. /* Init the hw */
  323. rv = mpc52xx_ata_hw_init(priv);
  324. if (rv) {
  325. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  326. goto err;
  327. }
  328. /* Register ourselves to libata */
  329. rv = mpc52xx_ata_init_one(&op->dev, priv, res_mem.start);
  330. if (rv) {
  331. printk(KERN_ERR DRV_NAME ": "
  332. "Error while registering to ATA layer\n");
  333. return rv;
  334. }
  335. /* Done */
  336. return 0;
  337. /* Error path */
  338. err:
  339. irq_dispose_mapping(ata_irq);
  340. return rv;
  341. }
  342. static int
  343. mpc52xx_ata_remove(struct of_device *op)
  344. {
  345. struct mpc52xx_ata_priv *priv;
  346. priv = mpc52xx_ata_remove_one(&op->dev);
  347. irq_dispose_mapping(priv->ata_irq);
  348. return 0;
  349. }
  350. #ifdef CONFIG_PM
  351. static int
  352. mpc52xx_ata_suspend(struct of_device *op, pm_message_t state)
  353. {
  354. struct ata_host *host = dev_get_drvdata(&op->dev);
  355. return ata_host_suspend(host, state);
  356. }
  357. static int
  358. mpc52xx_ata_resume(struct of_device *op)
  359. {
  360. struct ata_host *host = dev_get_drvdata(&op->dev);
  361. struct mpc52xx_ata_priv *priv = host->private_data;
  362. int rv;
  363. rv = mpc52xx_ata_hw_init(priv);
  364. if (rv) {
  365. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  366. return rv;
  367. }
  368. ata_host_resume(host);
  369. return 0;
  370. }
  371. #endif
  372. static struct of_device_id mpc52xx_ata_of_match[] = {
  373. { .compatible = "fsl,mpc5200-ata", },
  374. { .compatible = "mpc5200-ata", },
  375. {},
  376. };
  377. static struct of_platform_driver mpc52xx_ata_of_platform_driver = {
  378. .owner = THIS_MODULE,
  379. .name = DRV_NAME,
  380. .match_table = mpc52xx_ata_of_match,
  381. .probe = mpc52xx_ata_probe,
  382. .remove = mpc52xx_ata_remove,
  383. #ifdef CONFIG_PM
  384. .suspend = mpc52xx_ata_suspend,
  385. .resume = mpc52xx_ata_resume,
  386. #endif
  387. .driver = {
  388. .name = DRV_NAME,
  389. .owner = THIS_MODULE,
  390. },
  391. };
  392. /* ======================================================================== */
  393. /* Module */
  394. /* ======================================================================== */
  395. static int __init
  396. mpc52xx_ata_init(void)
  397. {
  398. printk(KERN_INFO "ata: MPC52xx IDE/ATA libata driver\n");
  399. return of_register_platform_driver(&mpc52xx_ata_of_platform_driver);
  400. }
  401. static void __exit
  402. mpc52xx_ata_exit(void)
  403. {
  404. of_unregister_platform_driver(&mpc52xx_ata_of_platform_driver);
  405. }
  406. module_init(mpc52xx_ata_init);
  407. module_exit(mpc52xx_ata_exit);
  408. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  409. MODULE_DESCRIPTION("Freescale MPC52xx IDE/ATA libata driver");
  410. MODULE_LICENSE("GPL");
  411. MODULE_DEVICE_TABLE(of, mpc52xx_ata_of_match);
  412. MODULE_VERSION(DRV_VERSION);