pata_mpc52xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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.0ac2"
  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. .cable_detect = ata_cable_40wire,
  241. .qc_prep = ata_qc_prep,
  242. .qc_issue = ata_qc_issue_prot,
  243. .data_xfer = ata_data_xfer,
  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 int __devinit
  250. mpc52xx_ata_init_one(struct device *dev, struct mpc52xx_ata_priv *priv)
  251. {
  252. struct ata_host *host;
  253. struct ata_port *ap;
  254. struct ata_ioports *aio;
  255. int rc;
  256. host = ata_host_alloc(dev, 1);
  257. if (!host)
  258. return -ENOMEM;
  259. ap = host->ports[0];
  260. ap->flags |= ATA_FLAG_SLAVE_POSS;
  261. ap->pio_mask = 0x1f; /* Up to PIO4 */
  262. ap->mwdma_mask = 0x00; /* No MWDMA */
  263. ap->udma_mask = 0x00; /* No UDMA */
  264. ap->ops = &mpc52xx_ata_port_ops;
  265. host->private_data = priv;
  266. aio = &ap->ioaddr;
  267. aio->cmd_addr = NULL; /* Don't have a classic reg block */
  268. aio->altstatus_addr = &priv->ata_regs->tf_control;
  269. aio->ctl_addr = &priv->ata_regs->tf_control;
  270. aio->data_addr = &priv->ata_regs->tf_data;
  271. aio->error_addr = &priv->ata_regs->tf_features;
  272. aio->feature_addr = &priv->ata_regs->tf_features;
  273. aio->nsect_addr = &priv->ata_regs->tf_sec_count;
  274. aio->lbal_addr = &priv->ata_regs->tf_sec_num;
  275. aio->lbam_addr = &priv->ata_regs->tf_cyl_low;
  276. aio->lbah_addr = &priv->ata_regs->tf_cyl_high;
  277. aio->device_addr = &priv->ata_regs->tf_dev_head;
  278. aio->status_addr = &priv->ata_regs->tf_command;
  279. aio->command_addr = &priv->ata_regs->tf_command;
  280. /* activate host */
  281. return ata_host_activate(host, priv->ata_irq, ata_interrupt, 0,
  282. &mpc52xx_ata_sht);
  283. }
  284. static struct mpc52xx_ata_priv *
  285. mpc52xx_ata_remove_one(struct device *dev)
  286. {
  287. struct ata_host *host = dev_get_drvdata(dev);
  288. struct mpc52xx_ata_priv *priv = host->private_data;
  289. ata_host_detach(host);
  290. return priv;
  291. }
  292. /* ======================================================================== */
  293. /* OF Platform driver */
  294. /* ======================================================================== */
  295. static int __devinit
  296. mpc52xx_ata_probe(struct of_device *op, const struct of_device_id *match)
  297. {
  298. unsigned int ipb_freq;
  299. struct resource res_mem;
  300. int ata_irq = NO_IRQ;
  301. struct mpc52xx_ata __iomem *ata_regs;
  302. struct mpc52xx_ata_priv *priv;
  303. int rv;
  304. /* Get ipb frequency */
  305. ipb_freq = mpc52xx_find_ipb_freq(op->node);
  306. if (!ipb_freq) {
  307. printk(KERN_ERR DRV_NAME ": "
  308. "Unable to find IPB Bus frequency\n" );
  309. return -ENODEV;
  310. }
  311. /* Get IRQ and register */
  312. rv = of_address_to_resource(op->node, 0, &res_mem);
  313. if (rv) {
  314. printk(KERN_ERR DRV_NAME ": "
  315. "Error while parsing device node resource\n" );
  316. return rv;
  317. }
  318. ata_irq = irq_of_parse_and_map(op->node, 0);
  319. if (ata_irq == NO_IRQ) {
  320. printk(KERN_ERR DRV_NAME ": "
  321. "Error while mapping the irq\n");
  322. return -EINVAL;
  323. }
  324. /* Request mem region */
  325. if (!devm_request_mem_region(&op->dev, res_mem.start,
  326. sizeof(struct mpc52xx_ata), DRV_NAME)) {
  327. printk(KERN_ERR DRV_NAME ": "
  328. "Error while requesting mem region\n");
  329. rv = -EBUSY;
  330. goto err;
  331. }
  332. /* Remap registers */
  333. ata_regs = devm_ioremap(&op->dev, res_mem.start,
  334. sizeof(struct mpc52xx_ata));
  335. if (!ata_regs) {
  336. printk(KERN_ERR DRV_NAME ": "
  337. "Error while mapping register set\n");
  338. rv = -ENOMEM;
  339. goto err;
  340. }
  341. /* Prepare our private structure */
  342. priv = devm_kzalloc(&op->dev, sizeof(struct mpc52xx_ata_priv),
  343. GFP_ATOMIC);
  344. if (!priv) {
  345. printk(KERN_ERR DRV_NAME ": "
  346. "Error while allocating private structure\n");
  347. rv = -ENOMEM;
  348. goto err;
  349. }
  350. priv->ipb_period = 1000000000 / (ipb_freq / 1000);
  351. priv->ata_regs = ata_regs;
  352. priv->ata_irq = ata_irq;
  353. priv->csel = -1;
  354. /* Init the hw */
  355. rv = mpc52xx_ata_hw_init(priv);
  356. if (rv) {
  357. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  358. goto err;
  359. }
  360. /* Register ourselves to libata */
  361. rv = mpc52xx_ata_init_one(&op->dev, priv);
  362. if (rv) {
  363. printk(KERN_ERR DRV_NAME ": "
  364. "Error while registering to ATA layer\n");
  365. return rv;
  366. }
  367. /* Done */
  368. return 0;
  369. /* Error path */
  370. err:
  371. irq_dispose_mapping(ata_irq);
  372. return rv;
  373. }
  374. static int
  375. mpc52xx_ata_remove(struct of_device *op)
  376. {
  377. struct mpc52xx_ata_priv *priv;
  378. priv = mpc52xx_ata_remove_one(&op->dev);
  379. irq_dispose_mapping(priv->ata_irq);
  380. return 0;
  381. }
  382. #ifdef CONFIG_PM
  383. static int
  384. mpc52xx_ata_suspend(struct of_device *op, pm_message_t state)
  385. {
  386. return 0; /* FIXME : What to do here ? */
  387. }
  388. static int
  389. mpc52xx_ata_resume(struct of_device *op)
  390. {
  391. return 0; /* FIXME : What to do here ? */
  392. }
  393. #endif
  394. static struct of_device_id mpc52xx_ata_of_match[] = {
  395. {
  396. .type = "ata",
  397. .compatible = "mpc5200-ata",
  398. },
  399. {},
  400. };
  401. static struct of_platform_driver mpc52xx_ata_of_platform_driver = {
  402. .owner = THIS_MODULE,
  403. .name = DRV_NAME,
  404. .match_table = mpc52xx_ata_of_match,
  405. .probe = mpc52xx_ata_probe,
  406. .remove = mpc52xx_ata_remove,
  407. #ifdef CONFIG_PM
  408. .suspend = mpc52xx_ata_suspend,
  409. .resume = mpc52xx_ata_resume,
  410. #endif
  411. .driver = {
  412. .name = DRV_NAME,
  413. .owner = THIS_MODULE,
  414. },
  415. };
  416. /* ======================================================================== */
  417. /* Module */
  418. /* ======================================================================== */
  419. static int __init
  420. mpc52xx_ata_init(void)
  421. {
  422. printk(KERN_INFO "ata: MPC52xx IDE/ATA libata driver\n");
  423. return of_register_platform_driver(&mpc52xx_ata_of_platform_driver);
  424. }
  425. static void __exit
  426. mpc52xx_ata_exit(void)
  427. {
  428. of_unregister_platform_driver(&mpc52xx_ata_of_platform_driver);
  429. }
  430. module_init(mpc52xx_ata_init);
  431. module_exit(mpc52xx_ata_exit);
  432. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  433. MODULE_DESCRIPTION("Freescale MPC52xx IDE/ATA libata driver");
  434. MODULE_LICENSE("GPL");
  435. MODULE_DEVICE_TABLE(of, mpc52xx_ata_of_match);
  436. MODULE_VERSION(DRV_VERSION);