sata_sx4.c 40 KB

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