pata_mpc52xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 <asm/types.h>
  19. #include <asm/prom.h>
  20. #include <asm/of_platform.h>
  21. #include <asm/mpc52xx.h>
  22. #define DRV_NAME "mpc52xx_ata"
  23. #define DRV_VERSION "0.1.0"
  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_std_dev_select(ap,device);
  205. }
  206. static void
  207. mpc52xx_ata_error_handler(struct ata_port *ap)
  208. {
  209. ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, NULL,
  210. ata_std_postreset);
  211. }
  212. static struct scsi_host_template mpc52xx_ata_sht = {
  213. .module = THIS_MODULE,
  214. .name = DRV_NAME,
  215. .ioctl = ata_scsi_ioctl,
  216. .queuecommand = ata_scsi_queuecmd,
  217. .can_queue = ATA_DEF_QUEUE,
  218. .this_id = ATA_SHT_THIS_ID,
  219. .sg_tablesize = LIBATA_MAX_PRD,
  220. .max_sectors = ATA_MAX_SECTORS,
  221. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  222. .emulated = ATA_SHT_EMULATED,
  223. .use_clustering = ATA_SHT_USE_CLUSTERING,
  224. .proc_name = DRV_NAME,
  225. .dma_boundary = ATA_DMA_BOUNDARY,
  226. .slave_configure = ata_scsi_slave_config,
  227. .bios_param = ata_std_bios_param,
  228. };
  229. static struct ata_port_operations mpc52xx_ata_port_ops = {
  230. .port_disable = ata_port_disable,
  231. .set_piomode = mpc52xx_ata_set_piomode,
  232. .dev_select = mpc52xx_ata_dev_select,
  233. .tf_load = ata_tf_load,
  234. .tf_read = ata_tf_read,
  235. .check_status = ata_check_status,
  236. .exec_command = ata_exec_command,
  237. .freeze = ata_bmdma_freeze,
  238. .thaw = ata_bmdma_thaw,
  239. .error_handler = mpc52xx_ata_error_handler,
  240. .qc_prep = ata_qc_prep,
  241. .qc_issue = ata_qc_issue_prot,
  242. .data_xfer = ata_data_xfer,
  243. .irq_handler = ata_interrupt,
  244. .irq_clear = ata_bmdma_irq_clear,
  245. .irq_on = ata_irq_on,
  246. .irq_ack = ata_irq_ack,
  247. .port_start = ata_port_start,
  248. };
  249. static struct ata_probe_ent mpc52xx_ata_probe_ent = {
  250. .port_ops = &mpc52xx_ata_port_ops,
  251. .sht = &mpc52xx_ata_sht,
  252. .n_ports = 1,
  253. .pio_mask = 0x1f, /* Up to PIO4 */
  254. .mwdma_mask = 0x00, /* No MWDMA */
  255. .udma_mask = 0x00, /* No UDMA */
  256. .port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  257. .irq_flags = 0,
  258. };
  259. static int __devinit
  260. mpc52xx_ata_init_one(struct device *dev, struct mpc52xx_ata_priv *priv)
  261. {
  262. struct ata_probe_ent *ae = &mpc52xx_ata_probe_ent;
  263. struct ata_ioports *aio = &ae->port[0];
  264. int rv;
  265. INIT_LIST_HEAD(&ae->node);
  266. ae->dev = dev;
  267. ae->irq = priv->ata_irq;
  268. aio->cmd_addr = 0; /* Don't have a classic reg block */
  269. aio->altstatus_addr = &priv->ata_regs->tf_control;
  270. aio->ctl_addr = &priv->ata_regs->tf_control;
  271. aio->data_addr = &priv->ata_regs->tf_data;
  272. aio->error_addr = &priv->ata_regs->tf_features;
  273. aio->feature_addr = &priv->ata_regs->tf_features;
  274. aio->nsect_addr = &priv->ata_regs->tf_sec_count;
  275. aio->lbal_addr = &priv->ata_regs->tf_sec_num;
  276. aio->lbam_addr = &priv->ata_regs->tf_cyl_low;
  277. aio->lbah_addr = &priv->ata_regs->tf_cyl_high;
  278. aio->device_addr = &priv->ata_regs->tf_dev_head;
  279. aio->status_addr = &priv->ata_regs->tf_command;
  280. aio->command_addr = &priv->ata_regs->tf_command;
  281. ae->private_data = priv;
  282. rv = ata_device_add(ae);
  283. return rv ? 0 : -EINVAL;
  284. }
  285. static struct mpc52xx_ata_priv *
  286. mpc52xx_ata_remove_one(struct device *dev)
  287. {
  288. struct ata_host *host = dev_get_drvdata(dev);
  289. struct mpc52xx_ata_priv *priv = host->private_data;
  290. ata_host_detach(host);
  291. return priv;
  292. }
  293. /* ======================================================================== */
  294. /* OF Platform driver */
  295. /* ======================================================================== */
  296. static int __devinit
  297. mpc52xx_ata_probe(struct of_device *op, const struct of_device_id *match)
  298. {
  299. unsigned int ipb_freq;
  300. struct resource res_mem;
  301. int ata_irq = NO_IRQ;
  302. struct mpc52xx_ata __iomem *ata_regs;
  303. struct mpc52xx_ata_priv *priv;
  304. int rv;
  305. /* Get ipb frequency */
  306. ipb_freq = mpc52xx_find_ipb_freq(op->node);
  307. if (!ipb_freq) {
  308. printk(KERN_ERR DRV_NAME ": "
  309. "Unable to find IPB Bus frequency\n" );
  310. return -ENODEV;
  311. }
  312. /* Get IRQ and register */
  313. rv = of_address_to_resource(op->node, 0, &res_mem);
  314. if (rv) {
  315. printk(KERN_ERR DRV_NAME ": "
  316. "Error while parsing device node resource\n" );
  317. return rv;
  318. }
  319. ata_irq = irq_of_parse_and_map(op->node, 0);
  320. if (ata_irq == NO_IRQ) {
  321. printk(KERN_ERR DRV_NAME ": "
  322. "Error while mapping the irq\n");
  323. return -EINVAL;
  324. }
  325. /* Request mem region */
  326. if (!devm_request_mem_region(&op->dev, res_mem.start,
  327. sizeof(struct mpc52xx_ata), DRV_NAME)) {
  328. printk(KERN_ERR DRV_NAME ": "
  329. "Error while requesting mem region\n");
  330. rv = -EBUSY;
  331. goto err;
  332. }
  333. /* Remap registers */
  334. ata_regs = devm_ioremap(&op->dev, res_mem.start,
  335. sizeof(struct mpc52xx_ata));
  336. if (!ata_regs) {
  337. printk(KERN_ERR DRV_NAME ": "
  338. "Error while mapping register set\n");
  339. rv = -ENOMEM;
  340. goto err;
  341. }
  342. /* Prepare our private structure */
  343. priv = devm_kzalloc(&op->dev, sizeof(struct mpc52xx_ata_priv),
  344. GFP_ATOMIC);
  345. if (!priv) {
  346. printk(KERN_ERR DRV_NAME ": "
  347. "Error while allocating private structure\n");
  348. rv = -ENOMEM;
  349. goto err;
  350. }
  351. priv->ipb_period = 1000000000 / (ipb_freq / 1000);
  352. priv->ata_regs = ata_regs;
  353. priv->ata_irq = ata_irq;
  354. priv->csel = -1;
  355. /* Init the hw */
  356. rv = mpc52xx_ata_hw_init(priv);
  357. if (rv) {
  358. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  359. goto err;
  360. }
  361. /* Register ourselves to libata */
  362. rv = mpc52xx_ata_init_one(&op->dev, priv);
  363. if (rv) {
  364. printk(KERN_ERR DRV_NAME ": "
  365. "Error while registering to ATA layer\n");
  366. return rv;
  367. }
  368. /* Done */
  369. return 0;
  370. /* Error path */
  371. err:
  372. irq_dispose_mapping(ata_irq);
  373. return rv;
  374. }
  375. static int
  376. mpc52xx_ata_remove(struct of_device *op)
  377. {
  378. struct mpc52xx_ata_priv *priv;
  379. priv = mpc52xx_ata_remove_one(&op->dev);
  380. irq_dispose_mapping(priv->ata_irq);
  381. return 0;
  382. }
  383. #ifdef CONFIG_PM
  384. static int
  385. mpc52xx_ata_suspend(struct of_device *op, pm_message_t state)
  386. {
  387. return 0; /* FIXME : What to do here ? */
  388. }
  389. static int
  390. mpc52xx_ata_resume(struct of_device *op)
  391. {
  392. return 0; /* FIXME : What to do here ? */
  393. }
  394. #endif
  395. static struct of_device_id mpc52xx_ata_of_match[] = {
  396. {
  397. .compatible = "mpc5200-ata",
  398. },
  399. {
  400. .compatible = "mpc52xx-ata",
  401. },
  402. {},
  403. };
  404. static struct of_platform_driver mpc52xx_ata_of_platform_driver = {
  405. .owner = THIS_MODULE,
  406. .name = DRV_NAME,
  407. .match_table = mpc52xx_ata_of_match,
  408. .probe = mpc52xx_ata_probe,
  409. .remove = mpc52xx_ata_remove,
  410. #ifdef CONFIG_PM
  411. .suspend = mpc52xx_ata_suspend,
  412. .resume = mpc52xx_ata_resume,
  413. #endif
  414. .driver = {
  415. .name = DRV_NAME,
  416. .owner = THIS_MODULE,
  417. },
  418. };
  419. /* ======================================================================== */
  420. /* Module */
  421. /* ======================================================================== */
  422. static int __init
  423. mpc52xx_ata_init(void)
  424. {
  425. printk(KERN_INFO "ata: MPC52xx IDE/ATA libata driver\n");
  426. return of_register_platform_driver(&mpc52xx_ata_of_platform_driver);
  427. }
  428. static void __exit
  429. mpc52xx_ata_exit(void)
  430. {
  431. of_unregister_platform_driver(&mpc52xx_ata_of_platform_driver);
  432. }
  433. module_init(mpc52xx_ata_init);
  434. module_exit(mpc52xx_ata_exit);
  435. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  436. MODULE_DESCRIPTION("Freescale MPC52xx IDE/ATA libata driver");
  437. MODULE_LICENSE("GPL");
  438. MODULE_DEVICE_TABLE(of, mpc52xx_ata_of_match);
  439. MODULE_VERSION(DRV_VERSION);