sata_sx4.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * sata_sx4.c - Promise SATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *
  26. * libata documentation is available via 'make {ps|pdf}docs',
  27. * as Documentation/DocBook/libata.*
  28. *
  29. * Hardware documentation available under NDA.
  30. *
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <linux/device.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <linux/libata.h>
  44. #include <asm/io.h>
  45. #include "sata_promise.h"
  46. #define DRV_NAME "sata_sx4"
  47. #define DRV_VERSION "0.8"
  48. enum {
  49. PDC_PRD_TBL = 0x44, /* Direct command DMA table addr */
  50. PDC_PKT_SUBMIT = 0x40, /* Command packet pointer addr */
  51. PDC_HDMA_PKT_SUBMIT = 0x100, /* Host DMA packet pointer addr */
  52. PDC_INT_SEQMASK = 0x40, /* Mask of asserted SEQ INTs */
  53. PDC_HDMA_CTLSTAT = 0x12C, /* Host DMA control / status */
  54. PDC_20621_SEQCTL = 0x400,
  55. PDC_20621_SEQMASK = 0x480,
  56. PDC_20621_GENERAL_CTL = 0x484,
  57. PDC_20621_PAGE_SIZE = (32 * 1024),
  58. /* chosen, not constant, values; we design our own DIMM mem map */
  59. PDC_20621_DIMM_WINDOW = 0x0C, /* page# for 32K DIMM window */
  60. PDC_20621_DIMM_BASE = 0x00200000,
  61. PDC_20621_DIMM_DATA = (64 * 1024),
  62. PDC_DIMM_DATA_STEP = (256 * 1024),
  63. PDC_DIMM_WINDOW_STEP = (8 * 1024),
  64. PDC_DIMM_HOST_PRD = (6 * 1024),
  65. PDC_DIMM_HOST_PKT = (128 * 0),
  66. PDC_DIMM_HPKT_PRD = (128 * 1),
  67. PDC_DIMM_ATA_PKT = (128 * 2),
  68. PDC_DIMM_APKT_PRD = (128 * 3),
  69. PDC_DIMM_HEADER_SZ = PDC_DIMM_APKT_PRD + 128,
  70. PDC_PAGE_WINDOW = 0x40,
  71. PDC_PAGE_DATA = PDC_PAGE_WINDOW +
  72. (PDC_20621_DIMM_DATA / PDC_20621_PAGE_SIZE),
  73. PDC_PAGE_SET = PDC_DIMM_DATA_STEP / PDC_20621_PAGE_SIZE,
  74. PDC_CHIP0_OFS = 0xC0000, /* offset of chip #0 */
  75. PDC_20621_ERR_MASK = (1<<19) | (1<<20) | (1<<21) | (1<<22) |
  76. (1<<23),
  77. board_20621 = 0, /* FastTrak S150 SX4 */
  78. PDC_RESET = (1 << 11), /* HDMA reset */
  79. PDC_MAX_HDMA = 32,
  80. PDC_HDMA_Q_MASK = (PDC_MAX_HDMA - 1),
  81. PDC_DIMM0_SPD_DEV_ADDRESS = 0x50,
  82. PDC_DIMM1_SPD_DEV_ADDRESS = 0x51,
  83. PDC_MAX_DIMM_MODULE = 0x02,
  84. PDC_I2C_CONTROL_OFFSET = 0x48,
  85. PDC_I2C_ADDR_DATA_OFFSET = 0x4C,
  86. PDC_DIMM0_CONTROL_OFFSET = 0x80,
  87. PDC_DIMM1_CONTROL_OFFSET = 0x84,
  88. PDC_SDRAM_CONTROL_OFFSET = 0x88,
  89. PDC_I2C_WRITE = 0x00000000,
  90. PDC_I2C_READ = 0x00000040,
  91. PDC_I2C_START = 0x00000080,
  92. PDC_I2C_MASK_INT = 0x00000020,
  93. PDC_I2C_COMPLETE = 0x00010000,
  94. PDC_I2C_NO_ACK = 0x00100000,
  95. PDC_DIMM_SPD_SUBADDRESS_START = 0x00,
  96. PDC_DIMM_SPD_SUBADDRESS_END = 0x7F,
  97. PDC_DIMM_SPD_ROW_NUM = 3,
  98. PDC_DIMM_SPD_COLUMN_NUM = 4,
  99. PDC_DIMM_SPD_MODULE_ROW = 5,
  100. PDC_DIMM_SPD_TYPE = 11,
  101. PDC_DIMM_SPD_FRESH_RATE = 12,
  102. PDC_DIMM_SPD_BANK_NUM = 17,
  103. PDC_DIMM_SPD_CAS_LATENCY = 18,
  104. PDC_DIMM_SPD_ATTRIBUTE = 21,
  105. PDC_DIMM_SPD_ROW_PRE_CHARGE = 27,
  106. PDC_DIMM_SPD_ROW_ACTIVE_DELAY = 28,
  107. PDC_DIMM_SPD_RAS_CAS_DELAY = 29,
  108. PDC_DIMM_SPD_ACTIVE_PRECHARGE = 30,
  109. PDC_DIMM_SPD_SYSTEM_FREQ = 126,
  110. PDC_CTL_STATUS = 0x08,
  111. PDC_DIMM_WINDOW_CTLR = 0x0C,
  112. PDC_TIME_CONTROL = 0x3C,
  113. PDC_TIME_PERIOD = 0x40,
  114. PDC_TIME_COUNTER = 0x44,
  115. PDC_GENERAL_CTLR = 0x484,
  116. PCI_PLL_INIT = 0x8A531824,
  117. PCI_X_TCOUNT = 0xEE1E5CFF
  118. };
  119. struct pdc_port_priv {
  120. u8 dimm_buf[(ATA_PRD_SZ * ATA_MAX_PRD) + 512];
  121. u8 *pkt;
  122. dma_addr_t pkt_dma;
  123. };
  124. struct pdc_host_priv {
  125. void __iomem *dimm_mmio;
  126. unsigned int doing_hdma;
  127. unsigned int hdma_prod;
  128. unsigned int hdma_cons;
  129. struct {
  130. struct ata_queued_cmd *qc;
  131. unsigned int seq;
  132. unsigned long pkt_ofs;
  133. } hdma[32];
  134. };
  135. static int pdc_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  136. static irqreturn_t pdc20621_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
  137. static void pdc_eng_timeout(struct ata_port *ap);
  138. static void pdc_20621_phy_reset (struct ata_port *ap);
  139. static int pdc_port_start(struct ata_port *ap);
  140. static void pdc_port_stop(struct ata_port *ap);
  141. static void pdc20621_qc_prep(struct ata_queued_cmd *qc);
  142. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  143. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  144. static void pdc20621_host_stop(struct ata_host_set *host_set);
  145. static unsigned int pdc20621_dimm_init(struct ata_probe_ent *pe);
  146. static int pdc20621_detect_dimm(struct ata_probe_ent *pe);
  147. static unsigned int pdc20621_i2c_read(struct ata_probe_ent *pe,
  148. u32 device, u32 subaddr, u32 *pdata);
  149. static int pdc20621_prog_dimm0(struct ata_probe_ent *pe);
  150. static unsigned int pdc20621_prog_dimm_global(struct ata_probe_ent *pe);
  151. #ifdef ATA_VERBOSE_DEBUG
  152. static void pdc20621_get_from_dimm(struct ata_probe_ent *pe,
  153. void *psource, u32 offset, u32 size);
  154. #endif
  155. static void pdc20621_put_to_dimm(struct ata_probe_ent *pe,
  156. void *psource, u32 offset, u32 size);
  157. static void pdc20621_irq_clear(struct ata_port *ap);
  158. static unsigned int pdc20621_qc_issue_prot(struct ata_queued_cmd *qc);
  159. static struct scsi_host_template pdc_sata_sht = {
  160. .module = THIS_MODULE,
  161. .name = DRV_NAME,
  162. .ioctl = ata_scsi_ioctl,
  163. .queuecommand = ata_scsi_queuecmd,
  164. .eh_timed_out = ata_scsi_timed_out,
  165. .eh_strategy_handler = ata_scsi_error,
  166. .can_queue = ATA_DEF_QUEUE,
  167. .this_id = ATA_SHT_THIS_ID,
  168. .sg_tablesize = LIBATA_MAX_PRD,
  169. .max_sectors = ATA_MAX_SECTORS,
  170. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  171. .emulated = ATA_SHT_EMULATED,
  172. .use_clustering = ATA_SHT_USE_CLUSTERING,
  173. .proc_name = DRV_NAME,
  174. .dma_boundary = ATA_DMA_BOUNDARY,
  175. .slave_configure = ata_scsi_slave_config,
  176. .bios_param = ata_std_bios_param,
  177. };
  178. static const struct ata_port_operations pdc_20621_ops = {
  179. .port_disable = ata_port_disable,
  180. .tf_load = pdc_tf_load_mmio,
  181. .tf_read = ata_tf_read,
  182. .check_status = ata_check_status,
  183. .exec_command = pdc_exec_command_mmio,
  184. .dev_select = ata_std_dev_select,
  185. .phy_reset = pdc_20621_phy_reset,
  186. .qc_prep = pdc20621_qc_prep,
  187. .qc_issue = pdc20621_qc_issue_prot,
  188. .eng_timeout = pdc_eng_timeout,
  189. .irq_handler = pdc20621_interrupt,
  190. .irq_clear = pdc20621_irq_clear,
  191. .port_start = pdc_port_start,
  192. .port_stop = pdc_port_stop,
  193. .host_stop = pdc20621_host_stop,
  194. };
  195. static const struct ata_port_info pdc_port_info[] = {
  196. /* board_20621 */
  197. {
  198. .sht = &pdc_sata_sht,
  199. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  200. ATA_FLAG_SRST | ATA_FLAG_MMIO |
  201. ATA_FLAG_NO_ATAPI,
  202. .pio_mask = 0x1f, /* pio0-4 */
  203. .mwdma_mask = 0x07, /* mwdma0-2 */
  204. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  205. .port_ops = &pdc_20621_ops,
  206. },
  207. };
  208. static const struct pci_device_id pdc_sata_pci_tbl[] = {
  209. { PCI_VENDOR_ID_PROMISE, 0x6622, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  210. board_20621 },
  211. { } /* terminate list */
  212. };
  213. static struct pci_driver pdc_sata_pci_driver = {
  214. .name = DRV_NAME,
  215. .id_table = pdc_sata_pci_tbl,
  216. .probe = pdc_sata_init_one,
  217. .remove = ata_pci_remove_one,
  218. };
  219. static void pdc20621_host_stop(struct ata_host_set *host_set)
  220. {
  221. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  222. struct pdc_host_priv *hpriv = host_set->private_data;
  223. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  224. pci_iounmap(pdev, dimm_mmio);
  225. kfree(hpriv);
  226. pci_iounmap(pdev, host_set->mmio_base);
  227. }
  228. static int pdc_port_start(struct ata_port *ap)
  229. {
  230. struct device *dev = ap->host_set->dev;
  231. struct pdc_port_priv *pp;
  232. int rc;
  233. rc = ata_port_start(ap);
  234. if (rc)
  235. return rc;
  236. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  237. if (!pp) {
  238. rc = -ENOMEM;
  239. goto err_out;
  240. }
  241. memset(pp, 0, sizeof(*pp));
  242. pp->pkt = dma_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  243. if (!pp->pkt) {
  244. rc = -ENOMEM;
  245. goto err_out_kfree;
  246. }
  247. ap->private_data = pp;
  248. return 0;
  249. err_out_kfree:
  250. kfree(pp);
  251. err_out:
  252. ata_port_stop(ap);
  253. return rc;
  254. }
  255. static void pdc_port_stop(struct ata_port *ap)
  256. {
  257. struct device *dev = ap->host_set->dev;
  258. struct pdc_port_priv *pp = ap->private_data;
  259. ap->private_data = NULL;
  260. dma_free_coherent(dev, 128, pp->pkt, pp->pkt_dma);
  261. kfree(pp);
  262. ata_port_stop(ap);
  263. }
  264. static void pdc_20621_phy_reset (struct ata_port *ap)
  265. {
  266. VPRINTK("ENTER\n");
  267. ap->cbl = ATA_CBL_SATA;
  268. ata_port_probe(ap);
  269. ata_bus_reset(ap);
  270. }
  271. static inline void pdc20621_ata_sg(struct ata_taskfile *tf, u8 *buf,
  272. unsigned int portno,
  273. unsigned int total_len)
  274. {
  275. u32 addr;
  276. unsigned int dw = PDC_DIMM_APKT_PRD >> 2;
  277. u32 *buf32 = (u32 *) buf;
  278. /* output ATA packet S/G table */
  279. addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA +
  280. (PDC_DIMM_DATA_STEP * portno);
  281. VPRINTK("ATA sg addr 0x%x, %d\n", addr, addr);
  282. buf32[dw] = cpu_to_le32(addr);
  283. buf32[dw + 1] = cpu_to_le32(total_len | ATA_PRD_EOT);
  284. VPRINTK("ATA PSG @ %x == (0x%x, 0x%x)\n",
  285. PDC_20621_DIMM_BASE +
  286. (PDC_DIMM_WINDOW_STEP * portno) +
  287. PDC_DIMM_APKT_PRD,
  288. buf32[dw], buf32[dw + 1]);
  289. }
  290. static inline void pdc20621_host_sg(struct ata_taskfile *tf, u8 *buf,
  291. unsigned int portno,
  292. unsigned int total_len)
  293. {
  294. u32 addr;
  295. unsigned int dw = PDC_DIMM_HPKT_PRD >> 2;
  296. u32 *buf32 = (u32 *) buf;
  297. /* output Host DMA packet S/G table */
  298. addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA +
  299. (PDC_DIMM_DATA_STEP * portno);
  300. buf32[dw] = cpu_to_le32(addr);
  301. buf32[dw + 1] = cpu_to_le32(total_len | ATA_PRD_EOT);
  302. VPRINTK("HOST PSG @ %x == (0x%x, 0x%x)\n",
  303. PDC_20621_DIMM_BASE +
  304. (PDC_DIMM_WINDOW_STEP * portno) +
  305. PDC_DIMM_HPKT_PRD,
  306. buf32[dw], buf32[dw + 1]);
  307. }
  308. static inline unsigned int pdc20621_ata_pkt(struct ata_taskfile *tf,
  309. unsigned int devno, u8 *buf,
  310. unsigned int portno)
  311. {
  312. unsigned int i, dw;
  313. u32 *buf32 = (u32 *) buf;
  314. u8 dev_reg;
  315. unsigned int dimm_sg = PDC_20621_DIMM_BASE +
  316. (PDC_DIMM_WINDOW_STEP * portno) +
  317. PDC_DIMM_APKT_PRD;
  318. VPRINTK("ENTER, dimm_sg == 0x%x, %d\n", dimm_sg, dimm_sg);
  319. i = PDC_DIMM_ATA_PKT;
  320. /*
  321. * Set up ATA packet
  322. */
  323. if ((tf->protocol == ATA_PROT_DMA) && (!(tf->flags & ATA_TFLAG_WRITE)))
  324. buf[i++] = PDC_PKT_READ;
  325. else if (tf->protocol == ATA_PROT_NODATA)
  326. buf[i++] = PDC_PKT_NODATA;
  327. else
  328. buf[i++] = 0;
  329. buf[i++] = 0; /* reserved */
  330. buf[i++] = portno + 1; /* seq. id */
  331. buf[i++] = 0xff; /* delay seq. id */
  332. /* dimm dma S/G, and next-pkt */
  333. dw = i >> 2;
  334. if (tf->protocol == ATA_PROT_NODATA)
  335. buf32[dw] = 0;
  336. else
  337. buf32[dw] = cpu_to_le32(dimm_sg);
  338. buf32[dw + 1] = 0;
  339. i += 8;
  340. if (devno == 0)
  341. dev_reg = ATA_DEVICE_OBS;
  342. else
  343. dev_reg = ATA_DEVICE_OBS | ATA_DEV1;
  344. /* select device */
  345. buf[i++] = (1 << 5) | PDC_PKT_CLEAR_BSY | ATA_REG_DEVICE;
  346. buf[i++] = dev_reg;
  347. /* device control register */
  348. buf[i++] = (1 << 5) | PDC_REG_DEVCTL;
  349. buf[i++] = tf->ctl;
  350. return i;
  351. }
  352. static inline void pdc20621_host_pkt(struct ata_taskfile *tf, u8 *buf,
  353. unsigned int portno)
  354. {
  355. unsigned int dw;
  356. u32 tmp, *buf32 = (u32 *) buf;
  357. unsigned int host_sg = PDC_20621_DIMM_BASE +
  358. (PDC_DIMM_WINDOW_STEP * portno) +
  359. PDC_DIMM_HOST_PRD;
  360. unsigned int dimm_sg = PDC_20621_DIMM_BASE +
  361. (PDC_DIMM_WINDOW_STEP * portno) +
  362. PDC_DIMM_HPKT_PRD;
  363. VPRINTK("ENTER, dimm_sg == 0x%x, %d\n", dimm_sg, dimm_sg);
  364. VPRINTK("host_sg == 0x%x, %d\n", host_sg, host_sg);
  365. dw = PDC_DIMM_HOST_PKT >> 2;
  366. /*
  367. * Set up Host DMA packet
  368. */
  369. if ((tf->protocol == ATA_PROT_DMA) && (!(tf->flags & ATA_TFLAG_WRITE)))
  370. tmp = PDC_PKT_READ;
  371. else
  372. tmp = 0;
  373. tmp |= ((portno + 1 + 4) << 16); /* seq. id */
  374. tmp |= (0xff << 24); /* delay seq. id */
  375. buf32[dw + 0] = cpu_to_le32(tmp);
  376. buf32[dw + 1] = cpu_to_le32(host_sg);
  377. buf32[dw + 2] = cpu_to_le32(dimm_sg);
  378. buf32[dw + 3] = 0;
  379. VPRINTK("HOST PKT @ %x == (0x%x 0x%x 0x%x 0x%x)\n",
  380. PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * portno) +
  381. PDC_DIMM_HOST_PKT,
  382. buf32[dw + 0],
  383. buf32[dw + 1],
  384. buf32[dw + 2],
  385. buf32[dw + 3]);
  386. }
  387. static void pdc20621_dma_prep(struct ata_queued_cmd *qc)
  388. {
  389. struct scatterlist *sg;
  390. struct ata_port *ap = qc->ap;
  391. struct pdc_port_priv *pp = ap->private_data;
  392. void __iomem *mmio = ap->host_set->mmio_base;
  393. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  394. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  395. unsigned int portno = ap->port_no;
  396. unsigned int i, idx, total_len = 0, sgt_len;
  397. u32 *buf = (u32 *) &pp->dimm_buf[PDC_DIMM_HEADER_SZ];
  398. assert(qc->flags & ATA_QCFLAG_DMAMAP);
  399. VPRINTK("ata%u: ENTER\n", ap->id);
  400. /* hard-code chip #0 */
  401. mmio += PDC_CHIP0_OFS;
  402. /*
  403. * Build S/G table
  404. */
  405. idx = 0;
  406. ata_for_each_sg(sg, qc) {
  407. buf[idx++] = cpu_to_le32(sg_dma_address(sg));
  408. buf[idx++] = cpu_to_le32(sg_dma_len(sg));
  409. total_len += sg_dma_len(sg);
  410. }
  411. buf[idx - 1] |= cpu_to_le32(ATA_PRD_EOT);
  412. sgt_len = idx * 4;
  413. /*
  414. * Build ATA, host DMA packets
  415. */
  416. pdc20621_host_sg(&qc->tf, &pp->dimm_buf[0], portno, total_len);
  417. pdc20621_host_pkt(&qc->tf, &pp->dimm_buf[0], portno);
  418. pdc20621_ata_sg(&qc->tf, &pp->dimm_buf[0], portno, total_len);
  419. i = pdc20621_ata_pkt(&qc->tf, qc->dev->devno, &pp->dimm_buf[0], portno);
  420. if (qc->tf.flags & ATA_TFLAG_LBA48)
  421. i = pdc_prep_lba48(&qc->tf, &pp->dimm_buf[0], i);
  422. else
  423. i = pdc_prep_lba28(&qc->tf, &pp->dimm_buf[0], i);
  424. pdc_pkt_footer(&qc->tf, &pp->dimm_buf[0], i);
  425. /* copy three S/G tables and two packets to DIMM MMIO window */
  426. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP),
  427. &pp->dimm_buf, PDC_DIMM_HEADER_SZ);
  428. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP) +
  429. PDC_DIMM_HOST_PRD,
  430. &pp->dimm_buf[PDC_DIMM_HEADER_SZ], sgt_len);
  431. /* force host FIFO dump */
  432. writel(0x00000001, mmio + PDC_20621_GENERAL_CTL);
  433. readl(dimm_mmio); /* MMIO PCI posting flush */
  434. VPRINTK("ata pkt buf ofs %u, prd size %u, mmio copied\n", i, sgt_len);
  435. }
  436. static void pdc20621_nodata_prep(struct ata_queued_cmd *qc)
  437. {
  438. struct ata_port *ap = qc->ap;
  439. struct pdc_port_priv *pp = ap->private_data;
  440. void __iomem *mmio = ap->host_set->mmio_base;
  441. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  442. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  443. unsigned int portno = ap->port_no;
  444. unsigned int i;
  445. VPRINTK("ata%u: ENTER\n", ap->id);
  446. /* hard-code chip #0 */
  447. mmio += PDC_CHIP0_OFS;
  448. i = pdc20621_ata_pkt(&qc->tf, qc->dev->devno, &pp->dimm_buf[0], portno);
  449. if (qc->tf.flags & ATA_TFLAG_LBA48)
  450. i = pdc_prep_lba48(&qc->tf, &pp->dimm_buf[0], i);
  451. else
  452. i = pdc_prep_lba28(&qc->tf, &pp->dimm_buf[0], i);
  453. pdc_pkt_footer(&qc->tf, &pp->dimm_buf[0], i);
  454. /* copy three S/G tables and two packets to DIMM MMIO window */
  455. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP),
  456. &pp->dimm_buf, PDC_DIMM_HEADER_SZ);
  457. /* force host FIFO dump */
  458. writel(0x00000001, mmio + PDC_20621_GENERAL_CTL);
  459. readl(dimm_mmio); /* MMIO PCI posting flush */
  460. VPRINTK("ata pkt buf ofs %u, mmio copied\n", i);
  461. }
  462. static void pdc20621_qc_prep(struct ata_queued_cmd *qc)
  463. {
  464. switch (qc->tf.protocol) {
  465. case ATA_PROT_DMA:
  466. pdc20621_dma_prep(qc);
  467. break;
  468. case ATA_PROT_NODATA:
  469. pdc20621_nodata_prep(qc);
  470. break;
  471. default:
  472. break;
  473. }
  474. }
  475. static void __pdc20621_push_hdma(struct ata_queued_cmd *qc,
  476. unsigned int seq,
  477. u32 pkt_ofs)
  478. {
  479. struct ata_port *ap = qc->ap;
  480. struct ata_host_set *host_set = ap->host_set;
  481. void __iomem *mmio = host_set->mmio_base;
  482. /* hard-code chip #0 */
  483. mmio += PDC_CHIP0_OFS;
  484. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  485. readl(mmio + PDC_20621_SEQCTL + (seq * 4)); /* flush */
  486. writel(pkt_ofs, mmio + PDC_HDMA_PKT_SUBMIT);
  487. readl(mmio + PDC_HDMA_PKT_SUBMIT); /* flush */
  488. }
  489. static void pdc20621_push_hdma(struct ata_queued_cmd *qc,
  490. unsigned int seq,
  491. u32 pkt_ofs)
  492. {
  493. struct ata_port *ap = qc->ap;
  494. struct pdc_host_priv *pp = ap->host_set->private_data;
  495. unsigned int idx = pp->hdma_prod & PDC_HDMA_Q_MASK;
  496. if (!pp->doing_hdma) {
  497. __pdc20621_push_hdma(qc, seq, pkt_ofs);
  498. pp->doing_hdma = 1;
  499. return;
  500. }
  501. pp->hdma[idx].qc = qc;
  502. pp->hdma[idx].seq = seq;
  503. pp->hdma[idx].pkt_ofs = pkt_ofs;
  504. pp->hdma_prod++;
  505. }
  506. static void pdc20621_pop_hdma(struct ata_queued_cmd *qc)
  507. {
  508. struct ata_port *ap = qc->ap;
  509. struct pdc_host_priv *pp = ap->host_set->private_data;
  510. unsigned int idx = pp->hdma_cons & PDC_HDMA_Q_MASK;
  511. /* if nothing on queue, we're done */
  512. if (pp->hdma_prod == pp->hdma_cons) {
  513. pp->doing_hdma = 0;
  514. return;
  515. }
  516. __pdc20621_push_hdma(pp->hdma[idx].qc, pp->hdma[idx].seq,
  517. pp->hdma[idx].pkt_ofs);
  518. pp->hdma_cons++;
  519. }
  520. #ifdef ATA_VERBOSE_DEBUG
  521. static void pdc20621_dump_hdma(struct ata_queued_cmd *qc)
  522. {
  523. struct ata_port *ap = qc->ap;
  524. unsigned int port_no = ap->port_no;
  525. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  526. void *dimm_mmio = hpriv->dimm_mmio;
  527. dimm_mmio += (port_no * PDC_DIMM_WINDOW_STEP);
  528. dimm_mmio += PDC_DIMM_HOST_PKT;
  529. printk(KERN_ERR "HDMA[0] == 0x%08X\n", readl(dimm_mmio));
  530. printk(KERN_ERR "HDMA[1] == 0x%08X\n", readl(dimm_mmio + 4));
  531. printk(KERN_ERR "HDMA[2] == 0x%08X\n", readl(dimm_mmio + 8));
  532. printk(KERN_ERR "HDMA[3] == 0x%08X\n", readl(dimm_mmio + 12));
  533. }
  534. #else
  535. static inline void pdc20621_dump_hdma(struct ata_queued_cmd *qc) { }
  536. #endif /* ATA_VERBOSE_DEBUG */
  537. static void pdc20621_packet_start(struct ata_queued_cmd *qc)
  538. {
  539. struct ata_port *ap = qc->ap;
  540. struct ata_host_set *host_set = ap->host_set;
  541. unsigned int port_no = ap->port_no;
  542. void __iomem *mmio = host_set->mmio_base;
  543. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  544. u8 seq = (u8) (port_no + 1);
  545. unsigned int port_ofs;
  546. /* hard-code chip #0 */
  547. mmio += PDC_CHIP0_OFS;
  548. VPRINTK("ata%u: ENTER\n", ap->id);
  549. wmb(); /* flush PRD, pkt writes */
  550. port_ofs = PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * port_no);
  551. /* if writing, we (1) DMA to DIMM, then (2) do ATA command */
  552. if (rw && qc->tf.protocol == ATA_PROT_DMA) {
  553. seq += 4;
  554. pdc20621_dump_hdma(qc);
  555. pdc20621_push_hdma(qc, seq, port_ofs + PDC_DIMM_HOST_PKT);
  556. VPRINTK("queued ofs 0x%x (%u), seq %u\n",
  557. port_ofs + PDC_DIMM_HOST_PKT,
  558. port_ofs + PDC_DIMM_HOST_PKT,
  559. seq);
  560. } else {
  561. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  562. readl(mmio + PDC_20621_SEQCTL + (seq * 4)); /* flush */
  563. writel(port_ofs + PDC_DIMM_ATA_PKT,
  564. (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  565. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  566. VPRINTK("submitted ofs 0x%x (%u), seq %u\n",
  567. port_ofs + PDC_DIMM_ATA_PKT,
  568. port_ofs + PDC_DIMM_ATA_PKT,
  569. seq);
  570. }
  571. }
  572. static unsigned int pdc20621_qc_issue_prot(struct ata_queued_cmd *qc)
  573. {
  574. switch (qc->tf.protocol) {
  575. case ATA_PROT_DMA:
  576. case ATA_PROT_NODATA:
  577. pdc20621_packet_start(qc);
  578. return 0;
  579. case ATA_PROT_ATAPI_DMA:
  580. BUG();
  581. break;
  582. default:
  583. break;
  584. }
  585. return ata_qc_issue_prot(qc);
  586. }
  587. static inline unsigned int pdc20621_host_intr( struct ata_port *ap,
  588. struct ata_queued_cmd *qc,
  589. unsigned int doing_hdma,
  590. void __iomem *mmio)
  591. {
  592. unsigned int port_no = ap->port_no;
  593. unsigned int port_ofs =
  594. PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * port_no);
  595. u8 status;
  596. unsigned int handled = 0;
  597. VPRINTK("ENTER\n");
  598. if ((qc->tf.protocol == ATA_PROT_DMA) && /* read */
  599. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  600. /* step two - DMA from DIMM to host */
  601. if (doing_hdma) {
  602. VPRINTK("ata%u: read hdma, 0x%x 0x%x\n", ap->id,
  603. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  604. /* get drive status; clear intr; complete txn */
  605. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  606. ata_qc_complete(qc);
  607. pdc20621_pop_hdma(qc);
  608. }
  609. /* step one - exec ATA command */
  610. else {
  611. u8 seq = (u8) (port_no + 1 + 4);
  612. VPRINTK("ata%u: read ata, 0x%x 0x%x\n", ap->id,
  613. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  614. /* submit hdma pkt */
  615. pdc20621_dump_hdma(qc);
  616. pdc20621_push_hdma(qc, seq,
  617. port_ofs + PDC_DIMM_HOST_PKT);
  618. }
  619. handled = 1;
  620. } else if (qc->tf.protocol == ATA_PROT_DMA) { /* write */
  621. /* step one - DMA from host to DIMM */
  622. if (doing_hdma) {
  623. u8 seq = (u8) (port_no + 1);
  624. VPRINTK("ata%u: write hdma, 0x%x 0x%x\n", ap->id,
  625. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  626. /* submit ata pkt */
  627. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  628. readl(mmio + PDC_20621_SEQCTL + (seq * 4));
  629. writel(port_ofs + PDC_DIMM_ATA_PKT,
  630. (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  631. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  632. }
  633. /* step two - execute ATA command */
  634. else {
  635. VPRINTK("ata%u: write ata, 0x%x 0x%x\n", ap->id,
  636. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  637. /* get drive status; clear intr; complete txn */
  638. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  639. ata_qc_complete(qc);
  640. pdc20621_pop_hdma(qc);
  641. }
  642. handled = 1;
  643. /* command completion, but no data xfer */
  644. } else if (qc->tf.protocol == ATA_PROT_NODATA) {
  645. status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  646. DPRINTK("BUS_NODATA (drv_stat 0x%X)\n", status);
  647. qc->err_mask |= ac_err_mask(status);
  648. ata_qc_complete(qc);
  649. handled = 1;
  650. } else {
  651. ap->stats.idle_irq++;
  652. }
  653. return handled;
  654. }
  655. static void pdc20621_irq_clear(struct ata_port *ap)
  656. {
  657. struct ata_host_set *host_set = ap->host_set;
  658. void __iomem *mmio = host_set->mmio_base;
  659. mmio += PDC_CHIP0_OFS;
  660. readl(mmio + PDC_20621_SEQMASK);
  661. }
  662. static irqreturn_t pdc20621_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  663. {
  664. struct ata_host_set *host_set = dev_instance;
  665. struct ata_port *ap;
  666. u32 mask = 0;
  667. unsigned int i, tmp, port_no;
  668. unsigned int handled = 0;
  669. void __iomem *mmio_base;
  670. VPRINTK("ENTER\n");
  671. if (!host_set || !host_set->mmio_base) {
  672. VPRINTK("QUICK EXIT\n");
  673. return IRQ_NONE;
  674. }
  675. mmio_base = host_set->mmio_base;
  676. /* reading should also clear interrupts */
  677. mmio_base += PDC_CHIP0_OFS;
  678. mask = readl(mmio_base + PDC_20621_SEQMASK);
  679. VPRINTK("mask == 0x%x\n", mask);
  680. if (mask == 0xffffffff) {
  681. VPRINTK("QUICK EXIT 2\n");
  682. return IRQ_NONE;
  683. }
  684. mask &= 0xffff; /* only 16 tags possible */
  685. if (!mask) {
  686. VPRINTK("QUICK EXIT 3\n");
  687. return IRQ_NONE;
  688. }
  689. spin_lock(&host_set->lock);
  690. for (i = 1; i < 9; i++) {
  691. port_no = i - 1;
  692. if (port_no > 3)
  693. port_no -= 4;
  694. if (port_no >= host_set->n_ports)
  695. ap = NULL;
  696. else
  697. ap = host_set->ports[port_no];
  698. tmp = mask & (1 << i);
  699. VPRINTK("seq %u, port_no %u, ap %p, tmp %x\n", i, port_no, ap, tmp);
  700. if (tmp && ap &&
  701. !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
  702. struct ata_queued_cmd *qc;
  703. qc = ata_qc_from_tag(ap, ap->active_tag);
  704. if (qc && (!(qc->tf.ctl & ATA_NIEN)))
  705. handled += pdc20621_host_intr(ap, qc, (i > 4),
  706. mmio_base);
  707. }
  708. }
  709. spin_unlock(&host_set->lock);
  710. VPRINTK("mask == 0x%x\n", mask);
  711. VPRINTK("EXIT\n");
  712. return IRQ_RETVAL(handled);
  713. }
  714. static void pdc_eng_timeout(struct ata_port *ap)
  715. {
  716. u8 drv_stat;
  717. struct ata_host_set *host_set = ap->host_set;
  718. struct ata_queued_cmd *qc;
  719. unsigned long flags;
  720. DPRINTK("ENTER\n");
  721. spin_lock_irqsave(&host_set->lock, flags);
  722. qc = ata_qc_from_tag(ap, ap->active_tag);
  723. if (!qc) {
  724. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  725. ap->id);
  726. goto out;
  727. }
  728. switch (qc->tf.protocol) {
  729. case ATA_PROT_DMA:
  730. case ATA_PROT_NODATA:
  731. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  732. qc->err_mask |= __ac_err_mask(ata_wait_idle(ap));
  733. break;
  734. default:
  735. drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  736. printk(KERN_ERR "ata%u: unknown timeout, cmd 0x%x stat 0x%x\n",
  737. ap->id, qc->tf.command, drv_stat);
  738. qc->err_mask |= ac_err_mask(drv_stat);
  739. break;
  740. }
  741. out:
  742. spin_unlock_irqrestore(&host_set->lock, flags);
  743. if (qc)
  744. ata_eh_qc_complete(qc);
  745. DPRINTK("EXIT\n");
  746. }
  747. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  748. {
  749. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  750. tf->protocol == ATA_PROT_NODATA);
  751. ata_tf_load(ap, tf);
  752. }
  753. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  754. {
  755. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  756. tf->protocol == ATA_PROT_NODATA);
  757. ata_exec_command(ap, tf);
  758. }
  759. static void pdc_sata_setup_port(struct ata_ioports *port, unsigned long base)
  760. {
  761. port->cmd_addr = base;
  762. port->data_addr = base;
  763. port->feature_addr =
  764. port->error_addr = base + 0x4;
  765. port->nsect_addr = base + 0x8;
  766. port->lbal_addr = base + 0xc;
  767. port->lbam_addr = base + 0x10;
  768. port->lbah_addr = base + 0x14;
  769. port->device_addr = base + 0x18;
  770. port->command_addr =
  771. port->status_addr = base + 0x1c;
  772. port->altstatus_addr =
  773. port->ctl_addr = base + 0x38;
  774. }
  775. #ifdef ATA_VERBOSE_DEBUG
  776. static void pdc20621_get_from_dimm(struct ata_probe_ent *pe, void *psource,
  777. u32 offset, u32 size)
  778. {
  779. u32 window_size;
  780. u16 idx;
  781. u8 page_mask;
  782. long dist;
  783. void __iomem *mmio = pe->mmio_base;
  784. struct pdc_host_priv *hpriv = pe->private_data;
  785. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  786. /* hard-code chip #0 */
  787. mmio += PDC_CHIP0_OFS;
  788. page_mask = 0x00;
  789. window_size = 0x2000 * 4; /* 32K byte uchar size */
  790. idx = (u16) (offset / window_size);
  791. writel(0x01, mmio + PDC_GENERAL_CTLR);
  792. readl(mmio + PDC_GENERAL_CTLR);
  793. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  794. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  795. offset -= (idx * window_size);
  796. idx++;
  797. dist = ((long) (window_size - (offset + size))) >= 0 ? size :
  798. (long) (window_size - offset);
  799. memcpy_fromio((char *) psource, (char *) (dimm_mmio + offset / 4),
  800. dist);
  801. psource += dist;
  802. size -= dist;
  803. for (; (long) size >= (long) window_size ;) {
  804. writel(0x01, mmio + PDC_GENERAL_CTLR);
  805. readl(mmio + PDC_GENERAL_CTLR);
  806. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  807. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  808. memcpy_fromio((char *) psource, (char *) (dimm_mmio),
  809. window_size / 4);
  810. psource += window_size;
  811. size -= window_size;
  812. idx ++;
  813. }
  814. if (size) {
  815. writel(0x01, mmio + PDC_GENERAL_CTLR);
  816. readl(mmio + PDC_GENERAL_CTLR);
  817. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  818. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  819. memcpy_fromio((char *) psource, (char *) (dimm_mmio),
  820. size / 4);
  821. }
  822. }
  823. #endif
  824. static void pdc20621_put_to_dimm(struct ata_probe_ent *pe, void *psource,
  825. u32 offset, u32 size)
  826. {
  827. u32 window_size;
  828. u16 idx;
  829. u8 page_mask;
  830. long dist;
  831. void __iomem *mmio = pe->mmio_base;
  832. struct pdc_host_priv *hpriv = pe->private_data;
  833. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  834. /* hard-code chip #0 */
  835. mmio += PDC_CHIP0_OFS;
  836. page_mask = 0x00;
  837. window_size = 0x2000 * 4; /* 32K byte uchar size */
  838. idx = (u16) (offset / window_size);
  839. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  840. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  841. offset -= (idx * window_size);
  842. idx++;
  843. dist = ((long)(s32)(window_size - (offset + size))) >= 0 ? size :
  844. (long) (window_size - offset);
  845. memcpy_toio(dimm_mmio + offset / 4, psource, dist);
  846. writel(0x01, mmio + PDC_GENERAL_CTLR);
  847. readl(mmio + PDC_GENERAL_CTLR);
  848. psource += dist;
  849. size -= dist;
  850. for (; (long) size >= (long) window_size ;) {
  851. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  852. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  853. memcpy_toio(dimm_mmio, psource, window_size / 4);
  854. writel(0x01, mmio + PDC_GENERAL_CTLR);
  855. readl(mmio + PDC_GENERAL_CTLR);
  856. psource += window_size;
  857. size -= window_size;
  858. idx ++;
  859. }
  860. if (size) {
  861. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  862. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  863. memcpy_toio(dimm_mmio, psource, size / 4);
  864. writel(0x01, mmio + PDC_GENERAL_CTLR);
  865. readl(mmio + PDC_GENERAL_CTLR);
  866. }
  867. }
  868. static unsigned int pdc20621_i2c_read(struct ata_probe_ent *pe, u32 device,
  869. u32 subaddr, u32 *pdata)
  870. {
  871. void __iomem *mmio = pe->mmio_base;
  872. u32 i2creg = 0;
  873. u32 status;
  874. u32 count =0;
  875. /* hard-code chip #0 */
  876. mmio += PDC_CHIP0_OFS;
  877. i2creg |= device << 24;
  878. i2creg |= subaddr << 16;
  879. /* Set the device and subaddress */
  880. writel(i2creg, mmio + PDC_I2C_ADDR_DATA_OFFSET);
  881. readl(mmio + PDC_I2C_ADDR_DATA_OFFSET);
  882. /* Write Control to perform read operation, mask int */
  883. writel(PDC_I2C_READ | PDC_I2C_START | PDC_I2C_MASK_INT,
  884. mmio + PDC_I2C_CONTROL_OFFSET);
  885. for (count = 0; count <= 1000; count ++) {
  886. status = readl(mmio + PDC_I2C_CONTROL_OFFSET);
  887. if (status & PDC_I2C_COMPLETE) {
  888. status = readl(mmio + PDC_I2C_ADDR_DATA_OFFSET);
  889. break;
  890. } else if (count == 1000)
  891. return 0;
  892. }
  893. *pdata = (status >> 8) & 0x000000ff;
  894. return 1;
  895. }
  896. static int pdc20621_detect_dimm(struct ata_probe_ent *pe)
  897. {
  898. u32 data=0 ;
  899. if (pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  900. PDC_DIMM_SPD_SYSTEM_FREQ, &data)) {
  901. if (data == 100)
  902. return 100;
  903. } else
  904. return 0;
  905. if (pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS, 9, &data)) {
  906. if(data <= 0x75)
  907. return 133;
  908. } else
  909. return 0;
  910. return 0;
  911. }
  912. static int pdc20621_prog_dimm0(struct ata_probe_ent *pe)
  913. {
  914. u32 spd0[50];
  915. u32 data = 0;
  916. int size, i;
  917. u8 bdimmsize;
  918. void __iomem *mmio = pe->mmio_base;
  919. static const struct {
  920. unsigned int reg;
  921. unsigned int ofs;
  922. } pdc_i2c_read_data [] = {
  923. { PDC_DIMM_SPD_TYPE, 11 },
  924. { PDC_DIMM_SPD_FRESH_RATE, 12 },
  925. { PDC_DIMM_SPD_COLUMN_NUM, 4 },
  926. { PDC_DIMM_SPD_ATTRIBUTE, 21 },
  927. { PDC_DIMM_SPD_ROW_NUM, 3 },
  928. { PDC_DIMM_SPD_BANK_NUM, 17 },
  929. { PDC_DIMM_SPD_MODULE_ROW, 5 },
  930. { PDC_DIMM_SPD_ROW_PRE_CHARGE, 27 },
  931. { PDC_DIMM_SPD_ROW_ACTIVE_DELAY, 28 },
  932. { PDC_DIMM_SPD_RAS_CAS_DELAY, 29 },
  933. { PDC_DIMM_SPD_ACTIVE_PRECHARGE, 30 },
  934. { PDC_DIMM_SPD_CAS_LATENCY, 18 },
  935. };
  936. /* hard-code chip #0 */
  937. mmio += PDC_CHIP0_OFS;
  938. for(i=0; i<ARRAY_SIZE(pdc_i2c_read_data); i++)
  939. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  940. pdc_i2c_read_data[i].reg,
  941. &spd0[pdc_i2c_read_data[i].ofs]);
  942. data |= (spd0[4] - 8) | ((spd0[21] != 0) << 3) | ((spd0[3]-11) << 4);
  943. data |= ((spd0[17] / 4) << 6) | ((spd0[5] / 2) << 7) |
  944. ((((spd0[27] + 9) / 10) - 1) << 8) ;
  945. data |= (((((spd0[29] > spd0[28])
  946. ? spd0[29] : spd0[28]) + 9) / 10) - 1) << 10;
  947. data |= ((spd0[30] - spd0[29] + 9) / 10 - 2) << 12;
  948. if (spd0[18] & 0x08)
  949. data |= ((0x03) << 14);
  950. else if (spd0[18] & 0x04)
  951. data |= ((0x02) << 14);
  952. else if (spd0[18] & 0x01)
  953. data |= ((0x01) << 14);
  954. else
  955. data |= (0 << 14);
  956. /*
  957. Calculate the size of bDIMMSize (power of 2) and
  958. merge the DIMM size by program start/end address.
  959. */
  960. bdimmsize = spd0[4] + (spd0[5] / 2) + spd0[3] + (spd0[17] / 2) + 3;
  961. size = (1 << bdimmsize) >> 20; /* size = xxx(MB) */
  962. data |= (((size / 16) - 1) << 16);
  963. data |= (0 << 23);
  964. data |= 8;
  965. writel(data, mmio + PDC_DIMM0_CONTROL_OFFSET);
  966. readl(mmio + PDC_DIMM0_CONTROL_OFFSET);
  967. return size;
  968. }
  969. static unsigned int pdc20621_prog_dimm_global(struct ata_probe_ent *pe)
  970. {
  971. u32 data, spd0;
  972. int error, i;
  973. void __iomem *mmio = pe->mmio_base;
  974. /* hard-code chip #0 */
  975. mmio += PDC_CHIP0_OFS;
  976. /*
  977. Set To Default : DIMM Module Global Control Register (0x022259F1)
  978. DIMM Arbitration Disable (bit 20)
  979. DIMM Data/Control Output Driving Selection (bit12 - bit15)
  980. Refresh Enable (bit 17)
  981. */
  982. data = 0x022259F1;
  983. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  984. readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  985. /* Turn on for ECC */
  986. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  987. PDC_DIMM_SPD_TYPE, &spd0);
  988. if (spd0 == 0x02) {
  989. data |= (0x01 << 16);
  990. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  991. readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  992. printk(KERN_ERR "Local DIMM ECC Enabled\n");
  993. }
  994. /* DIMM Initialization Select/Enable (bit 18/19) */
  995. data &= (~(1<<18));
  996. data |= (1<<19);
  997. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  998. error = 1;
  999. for (i = 1; i <= 10; i++) { /* polling ~5 secs */
  1000. data = readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  1001. if (!(data & (1<<19))) {
  1002. error = 0;
  1003. break;
  1004. }
  1005. msleep(i*100);
  1006. }
  1007. return error;
  1008. }
  1009. static unsigned int pdc20621_dimm_init(struct ata_probe_ent *pe)
  1010. {
  1011. int speed, size, length;
  1012. u32 addr,spd0,pci_status;
  1013. u32 tmp=0;
  1014. u32 time_period=0;
  1015. u32 tcount=0;
  1016. u32 ticks=0;
  1017. u32 clock=0;
  1018. u32 fparam=0;
  1019. void __iomem *mmio = pe->mmio_base;
  1020. /* hard-code chip #0 */
  1021. mmio += PDC_CHIP0_OFS;
  1022. /* Initialize PLL based upon PCI Bus Frequency */
  1023. /* Initialize Time Period Register */
  1024. writel(0xffffffff, mmio + PDC_TIME_PERIOD);
  1025. time_period = readl(mmio + PDC_TIME_PERIOD);
  1026. VPRINTK("Time Period Register (0x40): 0x%x\n", time_period);
  1027. /* Enable timer */
  1028. writel(0x00001a0, mmio + PDC_TIME_CONTROL);
  1029. readl(mmio + PDC_TIME_CONTROL);
  1030. /* Wait 3 seconds */
  1031. msleep(3000);
  1032. /*
  1033. When timer is enabled, counter is decreased every internal
  1034. clock cycle.
  1035. */
  1036. tcount = readl(mmio + PDC_TIME_COUNTER);
  1037. VPRINTK("Time Counter Register (0x44): 0x%x\n", tcount);
  1038. /*
  1039. If SX4 is on PCI-X bus, after 3 seconds, the timer counter
  1040. register should be >= (0xffffffff - 3x10^8).
  1041. */
  1042. if(tcount >= PCI_X_TCOUNT) {
  1043. ticks = (time_period - tcount);
  1044. VPRINTK("Num counters 0x%x (%d)\n", ticks, ticks);
  1045. clock = (ticks / 300000);
  1046. VPRINTK("10 * Internal clk = 0x%x (%d)\n", clock, clock);
  1047. clock = (clock * 33);
  1048. VPRINTK("10 * Internal clk * 33 = 0x%x (%d)\n", clock, clock);
  1049. /* PLL F Param (bit 22:16) */
  1050. fparam = (1400000 / clock) - 2;
  1051. VPRINTK("PLL F Param: 0x%x (%d)\n", fparam, fparam);
  1052. /* OD param = 0x2 (bit 31:30), R param = 0x5 (bit 29:25) */
  1053. pci_status = (0x8a001824 | (fparam << 16));
  1054. } else
  1055. pci_status = PCI_PLL_INIT;
  1056. /* Initialize PLL. */
  1057. VPRINTK("pci_status: 0x%x\n", pci_status);
  1058. writel(pci_status, mmio + PDC_CTL_STATUS);
  1059. readl(mmio + PDC_CTL_STATUS);
  1060. /*
  1061. Read SPD of DIMM by I2C interface,
  1062. and program the DIMM Module Controller.
  1063. */
  1064. if (!(speed = pdc20621_detect_dimm(pe))) {
  1065. printk(KERN_ERR "Detect Local DIMM Fail\n");
  1066. return 1; /* DIMM error */
  1067. }
  1068. VPRINTK("Local DIMM Speed = %d\n", speed);
  1069. /* Programming DIMM0 Module Control Register (index_CID0:80h) */
  1070. size = pdc20621_prog_dimm0(pe);
  1071. VPRINTK("Local DIMM Size = %dMB\n",size);
  1072. /* Programming DIMM Module Global Control Register (index_CID0:88h) */
  1073. if (pdc20621_prog_dimm_global(pe)) {
  1074. printk(KERN_ERR "Programming DIMM Module Global Control Register Fail\n");
  1075. return 1;
  1076. }
  1077. #ifdef ATA_VERBOSE_DEBUG
  1078. {
  1079. u8 test_parttern1[40] = {0x55,0xAA,'P','r','o','m','i','s','e',' ',
  1080. 'N','o','t',' ','Y','e','t',' ','D','e','f','i','n','e','d',' ',
  1081. '1','.','1','0',
  1082. '9','8','0','3','1','6','1','2',0,0};
  1083. u8 test_parttern2[40] = {0};
  1084. pdc20621_put_to_dimm(pe, (void *) test_parttern2, 0x10040, 40);
  1085. pdc20621_put_to_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1086. pdc20621_put_to_dimm(pe, (void *) test_parttern1, 0x10040, 40);
  1087. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1088. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1089. test_parttern2[1], &(test_parttern2[2]));
  1090. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x10040,
  1091. 40);
  1092. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1093. test_parttern2[1], &(test_parttern2[2]));
  1094. pdc20621_put_to_dimm(pe, (void *) test_parttern1, 0x40, 40);
  1095. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1096. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1097. test_parttern2[1], &(test_parttern2[2]));
  1098. }
  1099. #endif
  1100. /* ECC initiliazation. */
  1101. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  1102. PDC_DIMM_SPD_TYPE, &spd0);
  1103. if (spd0 == 0x02) {
  1104. VPRINTK("Start ECC initialization\n");
  1105. addr = 0;
  1106. length = size * 1024 * 1024;
  1107. while (addr < length) {
  1108. pdc20621_put_to_dimm(pe, (void *) &tmp, addr,
  1109. sizeof(u32));
  1110. addr += sizeof(u32);
  1111. }
  1112. VPRINTK("Finish ECC initialization\n");
  1113. }
  1114. return 0;
  1115. }
  1116. static void pdc_20621_init(struct ata_probe_ent *pe)
  1117. {
  1118. u32 tmp;
  1119. void __iomem *mmio = pe->mmio_base;
  1120. /* hard-code chip #0 */
  1121. mmio += PDC_CHIP0_OFS;
  1122. /*
  1123. * Select page 0x40 for our 32k DIMM window
  1124. */
  1125. tmp = readl(mmio + PDC_20621_DIMM_WINDOW) & 0xffff0000;
  1126. tmp |= PDC_PAGE_WINDOW; /* page 40h; arbitrarily selected */
  1127. writel(tmp, mmio + PDC_20621_DIMM_WINDOW);
  1128. /*
  1129. * Reset Host DMA
  1130. */
  1131. tmp = readl(mmio + PDC_HDMA_CTLSTAT);
  1132. tmp |= PDC_RESET;
  1133. writel(tmp, mmio + PDC_HDMA_CTLSTAT);
  1134. readl(mmio + PDC_HDMA_CTLSTAT); /* flush */
  1135. udelay(10);
  1136. tmp = readl(mmio + PDC_HDMA_CTLSTAT);
  1137. tmp &= ~PDC_RESET;
  1138. writel(tmp, mmio + PDC_HDMA_CTLSTAT);
  1139. readl(mmio + PDC_HDMA_CTLSTAT); /* flush */
  1140. }
  1141. static int pdc_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1142. {
  1143. static int printed_version;
  1144. struct ata_probe_ent *probe_ent = NULL;
  1145. unsigned long base;
  1146. void __iomem *mmio_base;
  1147. void __iomem *dimm_mmio = NULL;
  1148. struct pdc_host_priv *hpriv = NULL;
  1149. unsigned int board_idx = (unsigned int) ent->driver_data;
  1150. int pci_dev_busy = 0;
  1151. int rc;
  1152. if (!printed_version++)
  1153. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1154. /*
  1155. * If this driver happens to only be useful on Apple's K2, then
  1156. * we should check that here as it has a normal Serverworks ID
  1157. */
  1158. rc = pci_enable_device(pdev);
  1159. if (rc)
  1160. return rc;
  1161. rc = pci_request_regions(pdev, DRV_NAME);
  1162. if (rc) {
  1163. pci_dev_busy = 1;
  1164. goto err_out;
  1165. }
  1166. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  1167. if (rc)
  1168. goto err_out_regions;
  1169. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  1170. if (rc)
  1171. goto err_out_regions;
  1172. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1173. if (probe_ent == NULL) {
  1174. rc = -ENOMEM;
  1175. goto err_out_regions;
  1176. }
  1177. memset(probe_ent, 0, sizeof(*probe_ent));
  1178. probe_ent->dev = pci_dev_to_dev(pdev);
  1179. INIT_LIST_HEAD(&probe_ent->node);
  1180. mmio_base = pci_iomap(pdev, 3, 0);
  1181. if (mmio_base == NULL) {
  1182. rc = -ENOMEM;
  1183. goto err_out_free_ent;
  1184. }
  1185. base = (unsigned long) mmio_base;
  1186. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1187. if (!hpriv) {
  1188. rc = -ENOMEM;
  1189. goto err_out_iounmap;
  1190. }
  1191. memset(hpriv, 0, sizeof(*hpriv));
  1192. dimm_mmio = pci_iomap(pdev, 4, 0);
  1193. if (!dimm_mmio) {
  1194. kfree(hpriv);
  1195. rc = -ENOMEM;
  1196. goto err_out_iounmap;
  1197. }
  1198. hpriv->dimm_mmio = dimm_mmio;
  1199. probe_ent->sht = pdc_port_info[board_idx].sht;
  1200. probe_ent->host_flags = pdc_port_info[board_idx].host_flags;
  1201. probe_ent->pio_mask = pdc_port_info[board_idx].pio_mask;
  1202. probe_ent->mwdma_mask = pdc_port_info[board_idx].mwdma_mask;
  1203. probe_ent->udma_mask = pdc_port_info[board_idx].udma_mask;
  1204. probe_ent->port_ops = pdc_port_info[board_idx].port_ops;
  1205. probe_ent->irq = pdev->irq;
  1206. probe_ent->irq_flags = SA_SHIRQ;
  1207. probe_ent->mmio_base = mmio_base;
  1208. probe_ent->private_data = hpriv;
  1209. base += PDC_CHIP0_OFS;
  1210. probe_ent->n_ports = 4;
  1211. pdc_sata_setup_port(&probe_ent->port[0], base + 0x200);
  1212. pdc_sata_setup_port(&probe_ent->port[1], base + 0x280);
  1213. pdc_sata_setup_port(&probe_ent->port[2], base + 0x300);
  1214. pdc_sata_setup_port(&probe_ent->port[3], base + 0x380);
  1215. pci_set_master(pdev);
  1216. /* initialize adapter */
  1217. /* initialize local dimm */
  1218. if (pdc20621_dimm_init(probe_ent)) {
  1219. rc = -ENOMEM;
  1220. goto err_out_iounmap_dimm;
  1221. }
  1222. pdc_20621_init(probe_ent);
  1223. /* FIXME: check ata_device_add return value */
  1224. ata_device_add(probe_ent);
  1225. kfree(probe_ent);
  1226. return 0;
  1227. err_out_iounmap_dimm: /* only get to this label if 20621 */
  1228. kfree(hpriv);
  1229. pci_iounmap(pdev, dimm_mmio);
  1230. err_out_iounmap:
  1231. pci_iounmap(pdev, mmio_base);
  1232. err_out_free_ent:
  1233. kfree(probe_ent);
  1234. err_out_regions:
  1235. pci_release_regions(pdev);
  1236. err_out:
  1237. if (!pci_dev_busy)
  1238. pci_disable_device(pdev);
  1239. return rc;
  1240. }
  1241. static int __init pdc_sata_init(void)
  1242. {
  1243. return pci_module_init(&pdc_sata_pci_driver);
  1244. }
  1245. static void __exit pdc_sata_exit(void)
  1246. {
  1247. pci_unregister_driver(&pdc_sata_pci_driver);
  1248. }
  1249. MODULE_AUTHOR("Jeff Garzik");
  1250. MODULE_DESCRIPTION("Promise SATA low-level driver");
  1251. MODULE_LICENSE("GPL");
  1252. MODULE_DEVICE_TABLE(pci, pdc_sata_pci_tbl);
  1253. MODULE_VERSION(DRV_VERSION);
  1254. module_init(pdc_sata_init);
  1255. module_exit(pdc_sata_exit);