sata_sx4.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. .can_queue = ATA_DEF_QUEUE,
  165. .this_id = ATA_SHT_THIS_ID,
  166. .sg_tablesize = LIBATA_MAX_PRD,
  167. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  168. .emulated = ATA_SHT_EMULATED,
  169. .use_clustering = ATA_SHT_USE_CLUSTERING,
  170. .proc_name = DRV_NAME,
  171. .dma_boundary = ATA_DMA_BOUNDARY,
  172. .slave_configure = ata_scsi_slave_config,
  173. .bios_param = ata_std_bios_param,
  174. };
  175. static const struct ata_port_operations pdc_20621_ops = {
  176. .port_disable = ata_port_disable,
  177. .tf_load = pdc_tf_load_mmio,
  178. .tf_read = ata_tf_read,
  179. .check_status = ata_check_status,
  180. .exec_command = pdc_exec_command_mmio,
  181. .dev_select = ata_std_dev_select,
  182. .phy_reset = pdc_20621_phy_reset,
  183. .qc_prep = pdc20621_qc_prep,
  184. .qc_issue = pdc20621_qc_issue_prot,
  185. .eng_timeout = pdc_eng_timeout,
  186. .irq_handler = pdc20621_interrupt,
  187. .irq_clear = pdc20621_irq_clear,
  188. .port_start = pdc_port_start,
  189. .port_stop = pdc_port_stop,
  190. .host_stop = pdc20621_host_stop,
  191. };
  192. static const struct ata_port_info pdc_port_info[] = {
  193. /* board_20621 */
  194. {
  195. .sht = &pdc_sata_sht,
  196. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  197. ATA_FLAG_SRST | ATA_FLAG_MMIO |
  198. ATA_FLAG_NO_ATAPI,
  199. .pio_mask = 0x1f, /* pio0-4 */
  200. .mwdma_mask = 0x07, /* mwdma0-2 */
  201. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  202. .port_ops = &pdc_20621_ops,
  203. },
  204. };
  205. static const struct pci_device_id pdc_sata_pci_tbl[] = {
  206. { PCI_VENDOR_ID_PROMISE, 0x6622, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  207. board_20621 },
  208. { } /* terminate list */
  209. };
  210. static struct pci_driver pdc_sata_pci_driver = {
  211. .name = DRV_NAME,
  212. .id_table = pdc_sata_pci_tbl,
  213. .probe = pdc_sata_init_one,
  214. .remove = ata_pci_remove_one,
  215. };
  216. static void pdc20621_host_stop(struct ata_host_set *host_set)
  217. {
  218. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  219. struct pdc_host_priv *hpriv = host_set->private_data;
  220. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  221. pci_iounmap(pdev, dimm_mmio);
  222. kfree(hpriv);
  223. pci_iounmap(pdev, host_set->mmio_base);
  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;
  387. struct ata_port *ap = qc->ap;
  388. struct pdc_port_priv *pp = ap->private_data;
  389. void __iomem *mmio = ap->host_set->mmio_base;
  390. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  391. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  392. unsigned int portno = ap->port_no;
  393. unsigned int i, idx, total_len = 0, sgt_len;
  394. u32 *buf = (u32 *) &pp->dimm_buf[PDC_DIMM_HEADER_SZ];
  395. WARN_ON(!(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. idx = 0;
  403. ata_for_each_sg(sg, qc) {
  404. buf[idx++] = cpu_to_le32(sg_dma_address(sg));
  405. buf[idx++] = cpu_to_le32(sg_dma_len(sg));
  406. total_len += sg_dma_len(sg);
  407. }
  408. buf[idx - 1] |= cpu_to_le32(ATA_PRD_EOT);
  409. sgt_len = idx * 4;
  410. /*
  411. * Build ATA, host DMA packets
  412. */
  413. pdc20621_host_sg(&qc->tf, &pp->dimm_buf[0], portno, total_len);
  414. pdc20621_host_pkt(&qc->tf, &pp->dimm_buf[0], portno);
  415. pdc20621_ata_sg(&qc->tf, &pp->dimm_buf[0], portno, total_len);
  416. i = pdc20621_ata_pkt(&qc->tf, qc->dev->devno, &pp->dimm_buf[0], portno);
  417. if (qc->tf.flags & ATA_TFLAG_LBA48)
  418. i = pdc_prep_lba48(&qc->tf, &pp->dimm_buf[0], i);
  419. else
  420. i = pdc_prep_lba28(&qc->tf, &pp->dimm_buf[0], i);
  421. pdc_pkt_footer(&qc->tf, &pp->dimm_buf[0], i);
  422. /* copy three S/G tables and two packets to DIMM MMIO window */
  423. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP),
  424. &pp->dimm_buf, PDC_DIMM_HEADER_SZ);
  425. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP) +
  426. PDC_DIMM_HOST_PRD,
  427. &pp->dimm_buf[PDC_DIMM_HEADER_SZ], sgt_len);
  428. /* force host FIFO dump */
  429. writel(0x00000001, mmio + PDC_20621_GENERAL_CTL);
  430. readl(dimm_mmio); /* MMIO PCI posting flush */
  431. VPRINTK("ata pkt buf ofs %u, prd size %u, mmio copied\n", i, sgt_len);
  432. }
  433. static void pdc20621_nodata_prep(struct ata_queued_cmd *qc)
  434. {
  435. struct ata_port *ap = qc->ap;
  436. struct pdc_port_priv *pp = ap->private_data;
  437. void __iomem *mmio = ap->host_set->mmio_base;
  438. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  439. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  440. unsigned int portno = ap->port_no;
  441. unsigned int i;
  442. VPRINTK("ata%u: ENTER\n", ap->id);
  443. /* hard-code chip #0 */
  444. mmio += PDC_CHIP0_OFS;
  445. i = pdc20621_ata_pkt(&qc->tf, qc->dev->devno, &pp->dimm_buf[0], portno);
  446. if (qc->tf.flags & ATA_TFLAG_LBA48)
  447. i = pdc_prep_lba48(&qc->tf, &pp->dimm_buf[0], i);
  448. else
  449. i = pdc_prep_lba28(&qc->tf, &pp->dimm_buf[0], i);
  450. pdc_pkt_footer(&qc->tf, &pp->dimm_buf[0], i);
  451. /* copy three S/G tables and two packets to DIMM MMIO window */
  452. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP),
  453. &pp->dimm_buf, PDC_DIMM_HEADER_SZ);
  454. /* force host FIFO dump */
  455. writel(0x00000001, mmio + PDC_20621_GENERAL_CTL);
  456. readl(dimm_mmio); /* MMIO PCI posting flush */
  457. VPRINTK("ata pkt buf ofs %u, mmio copied\n", i);
  458. }
  459. static void pdc20621_qc_prep(struct ata_queued_cmd *qc)
  460. {
  461. switch (qc->tf.protocol) {
  462. case ATA_PROT_DMA:
  463. pdc20621_dma_prep(qc);
  464. break;
  465. case ATA_PROT_NODATA:
  466. pdc20621_nodata_prep(qc);
  467. break;
  468. default:
  469. break;
  470. }
  471. }
  472. static void __pdc20621_push_hdma(struct ata_queued_cmd *qc,
  473. unsigned int seq,
  474. u32 pkt_ofs)
  475. {
  476. struct ata_port *ap = qc->ap;
  477. struct ata_host_set *host_set = ap->host_set;
  478. void __iomem *mmio = host_set->mmio_base;
  479. /* hard-code chip #0 */
  480. mmio += PDC_CHIP0_OFS;
  481. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  482. readl(mmio + PDC_20621_SEQCTL + (seq * 4)); /* flush */
  483. writel(pkt_ofs, mmio + PDC_HDMA_PKT_SUBMIT);
  484. readl(mmio + PDC_HDMA_PKT_SUBMIT); /* flush */
  485. }
  486. static void pdc20621_push_hdma(struct ata_queued_cmd *qc,
  487. unsigned int seq,
  488. u32 pkt_ofs)
  489. {
  490. struct ata_port *ap = qc->ap;
  491. struct pdc_host_priv *pp = ap->host_set->private_data;
  492. unsigned int idx = pp->hdma_prod & PDC_HDMA_Q_MASK;
  493. if (!pp->doing_hdma) {
  494. __pdc20621_push_hdma(qc, seq, pkt_ofs);
  495. pp->doing_hdma = 1;
  496. return;
  497. }
  498. pp->hdma[idx].qc = qc;
  499. pp->hdma[idx].seq = seq;
  500. pp->hdma[idx].pkt_ofs = pkt_ofs;
  501. pp->hdma_prod++;
  502. }
  503. static void pdc20621_pop_hdma(struct ata_queued_cmd *qc)
  504. {
  505. struct ata_port *ap = qc->ap;
  506. struct pdc_host_priv *pp = ap->host_set->private_data;
  507. unsigned int idx = pp->hdma_cons & PDC_HDMA_Q_MASK;
  508. /* if nothing on queue, we're done */
  509. if (pp->hdma_prod == pp->hdma_cons) {
  510. pp->doing_hdma = 0;
  511. return;
  512. }
  513. __pdc20621_push_hdma(pp->hdma[idx].qc, pp->hdma[idx].seq,
  514. pp->hdma[idx].pkt_ofs);
  515. pp->hdma_cons++;
  516. }
  517. #ifdef ATA_VERBOSE_DEBUG
  518. static void pdc20621_dump_hdma(struct ata_queued_cmd *qc)
  519. {
  520. struct ata_port *ap = qc->ap;
  521. unsigned int port_no = ap->port_no;
  522. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  523. void *dimm_mmio = hpriv->dimm_mmio;
  524. dimm_mmio += (port_no * PDC_DIMM_WINDOW_STEP);
  525. dimm_mmio += PDC_DIMM_HOST_PKT;
  526. printk(KERN_ERR "HDMA[0] == 0x%08X\n", readl(dimm_mmio));
  527. printk(KERN_ERR "HDMA[1] == 0x%08X\n", readl(dimm_mmio + 4));
  528. printk(KERN_ERR "HDMA[2] == 0x%08X\n", readl(dimm_mmio + 8));
  529. printk(KERN_ERR "HDMA[3] == 0x%08X\n", readl(dimm_mmio + 12));
  530. }
  531. #else
  532. static inline void pdc20621_dump_hdma(struct ata_queued_cmd *qc) { }
  533. #endif /* ATA_VERBOSE_DEBUG */
  534. static void pdc20621_packet_start(struct ata_queued_cmd *qc)
  535. {
  536. struct ata_port *ap = qc->ap;
  537. struct ata_host_set *host_set = ap->host_set;
  538. unsigned int port_no = ap->port_no;
  539. void __iomem *mmio = host_set->mmio_base;
  540. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  541. u8 seq = (u8) (port_no + 1);
  542. unsigned int port_ofs;
  543. /* hard-code chip #0 */
  544. mmio += PDC_CHIP0_OFS;
  545. VPRINTK("ata%u: ENTER\n", ap->id);
  546. wmb(); /* flush PRD, pkt writes */
  547. port_ofs = PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * port_no);
  548. /* if writing, we (1) DMA to DIMM, then (2) do ATA command */
  549. if (rw && qc->tf.protocol == ATA_PROT_DMA) {
  550. seq += 4;
  551. pdc20621_dump_hdma(qc);
  552. pdc20621_push_hdma(qc, seq, port_ofs + PDC_DIMM_HOST_PKT);
  553. VPRINTK("queued ofs 0x%x (%u), seq %u\n",
  554. port_ofs + PDC_DIMM_HOST_PKT,
  555. port_ofs + PDC_DIMM_HOST_PKT,
  556. seq);
  557. } else {
  558. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  559. readl(mmio + PDC_20621_SEQCTL + (seq * 4)); /* flush */
  560. writel(port_ofs + PDC_DIMM_ATA_PKT,
  561. (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  562. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  563. VPRINTK("submitted ofs 0x%x (%u), seq %u\n",
  564. port_ofs + PDC_DIMM_ATA_PKT,
  565. port_ofs + PDC_DIMM_ATA_PKT,
  566. seq);
  567. }
  568. }
  569. static unsigned int pdc20621_qc_issue_prot(struct ata_queued_cmd *qc)
  570. {
  571. switch (qc->tf.protocol) {
  572. case ATA_PROT_DMA:
  573. case ATA_PROT_NODATA:
  574. pdc20621_packet_start(qc);
  575. return 0;
  576. case ATA_PROT_ATAPI_DMA:
  577. BUG();
  578. break;
  579. default:
  580. break;
  581. }
  582. return ata_qc_issue_prot(qc);
  583. }
  584. static inline unsigned int pdc20621_host_intr( struct ata_port *ap,
  585. struct ata_queued_cmd *qc,
  586. unsigned int doing_hdma,
  587. void __iomem *mmio)
  588. {
  589. unsigned int port_no = ap->port_no;
  590. unsigned int port_ofs =
  591. PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * port_no);
  592. u8 status;
  593. unsigned int handled = 0;
  594. VPRINTK("ENTER\n");
  595. if ((qc->tf.protocol == ATA_PROT_DMA) && /* read */
  596. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  597. /* step two - DMA from DIMM to host */
  598. if (doing_hdma) {
  599. VPRINTK("ata%u: read hdma, 0x%x 0x%x\n", ap->id,
  600. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  601. /* get drive status; clear intr; complete txn */
  602. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  603. ata_qc_complete(qc);
  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 __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  628. readl((void __iomem *) 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. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  636. ata_qc_complete(qc);
  637. pdc20621_pop_hdma(qc);
  638. }
  639. handled = 1;
  640. /* command completion, but no data xfer */
  641. } else if (qc->tf.protocol == ATA_PROT_NODATA) {
  642. status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  643. DPRINTK("BUS_NODATA (drv_stat 0x%X)\n", status);
  644. qc->err_mask |= ac_err_mask(status);
  645. ata_qc_complete(qc);
  646. handled = 1;
  647. } else {
  648. ap->stats.idle_irq++;
  649. }
  650. return handled;
  651. }
  652. static void pdc20621_irq_clear(struct ata_port *ap)
  653. {
  654. struct ata_host_set *host_set = ap->host_set;
  655. void __iomem *mmio = host_set->mmio_base;
  656. mmio += PDC_CHIP0_OFS;
  657. readl(mmio + PDC_20621_SEQMASK);
  658. }
  659. static irqreturn_t pdc20621_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  660. {
  661. struct ata_host_set *host_set = dev_instance;
  662. struct ata_port *ap;
  663. u32 mask = 0;
  664. unsigned int i, tmp, port_no;
  665. unsigned int handled = 0;
  666. void __iomem *mmio_base;
  667. VPRINTK("ENTER\n");
  668. if (!host_set || !host_set->mmio_base) {
  669. VPRINTK("QUICK EXIT\n");
  670. return IRQ_NONE;
  671. }
  672. mmio_base = host_set->mmio_base;
  673. /* reading should also clear interrupts */
  674. mmio_base += PDC_CHIP0_OFS;
  675. mask = readl(mmio_base + PDC_20621_SEQMASK);
  676. VPRINTK("mask == 0x%x\n", mask);
  677. if (mask == 0xffffffff) {
  678. VPRINTK("QUICK EXIT 2\n");
  679. return IRQ_NONE;
  680. }
  681. mask &= 0xffff; /* only 16 tags possible */
  682. if (!mask) {
  683. VPRINTK("QUICK EXIT 3\n");
  684. return IRQ_NONE;
  685. }
  686. spin_lock(&host_set->lock);
  687. for (i = 1; i < 9; i++) {
  688. port_no = i - 1;
  689. if (port_no > 3)
  690. port_no -= 4;
  691. if (port_no >= host_set->n_ports)
  692. ap = NULL;
  693. else
  694. ap = host_set->ports[port_no];
  695. tmp = mask & (1 << i);
  696. VPRINTK("seq %u, port_no %u, ap %p, tmp %x\n", i, port_no, ap, tmp);
  697. if (tmp && ap &&
  698. !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
  699. struct ata_queued_cmd *qc;
  700. qc = ata_qc_from_tag(ap, ap->active_tag);
  701. if (qc && (!(qc->tf.ctl & ATA_NIEN)))
  702. handled += pdc20621_host_intr(ap, qc, (i > 4),
  703. mmio_base);
  704. }
  705. }
  706. spin_unlock(&host_set->lock);
  707. VPRINTK("mask == 0x%x\n", mask);
  708. VPRINTK("EXIT\n");
  709. return IRQ_RETVAL(handled);
  710. }
  711. static void pdc_eng_timeout(struct ata_port *ap)
  712. {
  713. u8 drv_stat;
  714. struct ata_host_set *host_set = ap->host_set;
  715. struct ata_queued_cmd *qc;
  716. unsigned long flags;
  717. DPRINTK("ENTER\n");
  718. spin_lock_irqsave(&host_set->lock, flags);
  719. qc = ata_qc_from_tag(ap, ap->active_tag);
  720. switch (qc->tf.protocol) {
  721. case ATA_PROT_DMA:
  722. case ATA_PROT_NODATA:
  723. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  724. qc->err_mask |= __ac_err_mask(ata_wait_idle(ap));
  725. break;
  726. default:
  727. drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  728. printk(KERN_ERR "ata%u: unknown timeout, cmd 0x%x stat 0x%x\n",
  729. ap->id, qc->tf.command, drv_stat);
  730. qc->err_mask |= ac_err_mask(drv_stat);
  731. break;
  732. }
  733. spin_unlock_irqrestore(&host_set->lock, flags);
  734. ata_eh_qc_complete(qc);
  735. DPRINTK("EXIT\n");
  736. }
  737. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  738. {
  739. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  740. tf->protocol == ATA_PROT_NODATA);
  741. ata_tf_load(ap, tf);
  742. }
  743. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  744. {
  745. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  746. tf->protocol == ATA_PROT_NODATA);
  747. ata_exec_command(ap, tf);
  748. }
  749. static void pdc_sata_setup_port(struct ata_ioports *port, unsigned long base)
  750. {
  751. port->cmd_addr = base;
  752. port->data_addr = base;
  753. port->feature_addr =
  754. port->error_addr = base + 0x4;
  755. port->nsect_addr = base + 0x8;
  756. port->lbal_addr = base + 0xc;
  757. port->lbam_addr = base + 0x10;
  758. port->lbah_addr = base + 0x14;
  759. port->device_addr = base + 0x18;
  760. port->command_addr =
  761. port->status_addr = base + 0x1c;
  762. port->altstatus_addr =
  763. port->ctl_addr = base + 0x38;
  764. }
  765. #ifdef ATA_VERBOSE_DEBUG
  766. static void pdc20621_get_from_dimm(struct ata_probe_ent *pe, void *psource,
  767. u32 offset, u32 size)
  768. {
  769. u32 window_size;
  770. u16 idx;
  771. u8 page_mask;
  772. long dist;
  773. void __iomem *mmio = pe->mmio_base;
  774. struct pdc_host_priv *hpriv = pe->private_data;
  775. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  776. /* hard-code chip #0 */
  777. mmio += PDC_CHIP0_OFS;
  778. page_mask = 0x00;
  779. window_size = 0x2000 * 4; /* 32K byte uchar size */
  780. idx = (u16) (offset / window_size);
  781. writel(0x01, mmio + PDC_GENERAL_CTLR);
  782. readl(mmio + PDC_GENERAL_CTLR);
  783. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  784. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  785. offset -= (idx * window_size);
  786. idx++;
  787. dist = ((long) (window_size - (offset + size))) >= 0 ? size :
  788. (long) (window_size - offset);
  789. memcpy_fromio((char *) psource, (char *) (dimm_mmio + offset / 4),
  790. dist);
  791. psource += dist;
  792. size -= dist;
  793. for (; (long) size >= (long) window_size ;) {
  794. writel(0x01, mmio + PDC_GENERAL_CTLR);
  795. readl(mmio + PDC_GENERAL_CTLR);
  796. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  797. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  798. memcpy_fromio((char *) psource, (char *) (dimm_mmio),
  799. window_size / 4);
  800. psource += window_size;
  801. size -= window_size;
  802. idx ++;
  803. }
  804. if (size) {
  805. writel(0x01, mmio + PDC_GENERAL_CTLR);
  806. readl(mmio + PDC_GENERAL_CTLR);
  807. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  808. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  809. memcpy_fromio((char *) psource, (char *) (dimm_mmio),
  810. size / 4);
  811. }
  812. }
  813. #endif
  814. static void pdc20621_put_to_dimm(struct ata_probe_ent *pe, void *psource,
  815. u32 offset, u32 size)
  816. {
  817. u32 window_size;
  818. u16 idx;
  819. u8 page_mask;
  820. long dist;
  821. void __iomem *mmio = pe->mmio_base;
  822. struct pdc_host_priv *hpriv = pe->private_data;
  823. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  824. /* hard-code chip #0 */
  825. mmio += PDC_CHIP0_OFS;
  826. page_mask = 0x00;
  827. window_size = 0x2000 * 4; /* 32K byte uchar size */
  828. idx = (u16) (offset / window_size);
  829. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  830. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  831. offset -= (idx * window_size);
  832. idx++;
  833. dist = ((long)(s32)(window_size - (offset + size))) >= 0 ? size :
  834. (long) (window_size - offset);
  835. memcpy_toio(dimm_mmio + offset / 4, psource, dist);
  836. writel(0x01, mmio + PDC_GENERAL_CTLR);
  837. readl(mmio + PDC_GENERAL_CTLR);
  838. psource += dist;
  839. size -= dist;
  840. for (; (long) size >= (long) window_size ;) {
  841. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  842. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  843. memcpy_toio(dimm_mmio, psource, window_size / 4);
  844. writel(0x01, mmio + PDC_GENERAL_CTLR);
  845. readl(mmio + PDC_GENERAL_CTLR);
  846. psource += window_size;
  847. size -= window_size;
  848. idx ++;
  849. }
  850. if (size) {
  851. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  852. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  853. memcpy_toio(dimm_mmio, psource, size / 4);
  854. writel(0x01, mmio + PDC_GENERAL_CTLR);
  855. readl(mmio + PDC_GENERAL_CTLR);
  856. }
  857. }
  858. static unsigned int pdc20621_i2c_read(struct ata_probe_ent *pe, u32 device,
  859. u32 subaddr, u32 *pdata)
  860. {
  861. void __iomem *mmio = pe->mmio_base;
  862. u32 i2creg = 0;
  863. u32 status;
  864. u32 count =0;
  865. /* hard-code chip #0 */
  866. mmio += PDC_CHIP0_OFS;
  867. i2creg |= device << 24;
  868. i2creg |= subaddr << 16;
  869. /* Set the device and subaddress */
  870. writel(i2creg, mmio + PDC_I2C_ADDR_DATA_OFFSET);
  871. readl(mmio + PDC_I2C_ADDR_DATA_OFFSET);
  872. /* Write Control to perform read operation, mask int */
  873. writel(PDC_I2C_READ | PDC_I2C_START | PDC_I2C_MASK_INT,
  874. mmio + PDC_I2C_CONTROL_OFFSET);
  875. for (count = 0; count <= 1000; count ++) {
  876. status = readl(mmio + PDC_I2C_CONTROL_OFFSET);
  877. if (status & PDC_I2C_COMPLETE) {
  878. status = readl(mmio + PDC_I2C_ADDR_DATA_OFFSET);
  879. break;
  880. } else if (count == 1000)
  881. return 0;
  882. }
  883. *pdata = (status >> 8) & 0x000000ff;
  884. return 1;
  885. }
  886. static int pdc20621_detect_dimm(struct ata_probe_ent *pe)
  887. {
  888. u32 data=0 ;
  889. if (pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  890. PDC_DIMM_SPD_SYSTEM_FREQ, &data)) {
  891. if (data == 100)
  892. return 100;
  893. } else
  894. return 0;
  895. if (pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS, 9, &data)) {
  896. if(data <= 0x75)
  897. return 133;
  898. } else
  899. return 0;
  900. return 0;
  901. }
  902. static int pdc20621_prog_dimm0(struct ata_probe_ent *pe)
  903. {
  904. u32 spd0[50];
  905. u32 data = 0;
  906. int size, i;
  907. u8 bdimmsize;
  908. void __iomem *mmio = pe->mmio_base;
  909. static const struct {
  910. unsigned int reg;
  911. unsigned int ofs;
  912. } pdc_i2c_read_data [] = {
  913. { PDC_DIMM_SPD_TYPE, 11 },
  914. { PDC_DIMM_SPD_FRESH_RATE, 12 },
  915. { PDC_DIMM_SPD_COLUMN_NUM, 4 },
  916. { PDC_DIMM_SPD_ATTRIBUTE, 21 },
  917. { PDC_DIMM_SPD_ROW_NUM, 3 },
  918. { PDC_DIMM_SPD_BANK_NUM, 17 },
  919. { PDC_DIMM_SPD_MODULE_ROW, 5 },
  920. { PDC_DIMM_SPD_ROW_PRE_CHARGE, 27 },
  921. { PDC_DIMM_SPD_ROW_ACTIVE_DELAY, 28 },
  922. { PDC_DIMM_SPD_RAS_CAS_DELAY, 29 },
  923. { PDC_DIMM_SPD_ACTIVE_PRECHARGE, 30 },
  924. { PDC_DIMM_SPD_CAS_LATENCY, 18 },
  925. };
  926. /* hard-code chip #0 */
  927. mmio += PDC_CHIP0_OFS;
  928. for(i=0; i<ARRAY_SIZE(pdc_i2c_read_data); i++)
  929. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  930. pdc_i2c_read_data[i].reg,
  931. &spd0[pdc_i2c_read_data[i].ofs]);
  932. data |= (spd0[4] - 8) | ((spd0[21] != 0) << 3) | ((spd0[3]-11) << 4);
  933. data |= ((spd0[17] / 4) << 6) | ((spd0[5] / 2) << 7) |
  934. ((((spd0[27] + 9) / 10) - 1) << 8) ;
  935. data |= (((((spd0[29] > spd0[28])
  936. ? spd0[29] : spd0[28]) + 9) / 10) - 1) << 10;
  937. data |= ((spd0[30] - spd0[29] + 9) / 10 - 2) << 12;
  938. if (spd0[18] & 0x08)
  939. data |= ((0x03) << 14);
  940. else if (spd0[18] & 0x04)
  941. data |= ((0x02) << 14);
  942. else if (spd0[18] & 0x01)
  943. data |= ((0x01) << 14);
  944. else
  945. data |= (0 << 14);
  946. /*
  947. Calculate the size of bDIMMSize (power of 2) and
  948. merge the DIMM size by program start/end address.
  949. */
  950. bdimmsize = spd0[4] + (spd0[5] / 2) + spd0[3] + (spd0[17] / 2) + 3;
  951. size = (1 << bdimmsize) >> 20; /* size = xxx(MB) */
  952. data |= (((size / 16) - 1) << 16);
  953. data |= (0 << 23);
  954. data |= 8;
  955. writel(data, mmio + PDC_DIMM0_CONTROL_OFFSET);
  956. readl(mmio + PDC_DIMM0_CONTROL_OFFSET);
  957. return size;
  958. }
  959. static unsigned int pdc20621_prog_dimm_global(struct ata_probe_ent *pe)
  960. {
  961. u32 data, spd0;
  962. int error, i;
  963. void __iomem *mmio = pe->mmio_base;
  964. /* hard-code chip #0 */
  965. mmio += PDC_CHIP0_OFS;
  966. /*
  967. Set To Default : DIMM Module Global Control Register (0x022259F1)
  968. DIMM Arbitration Disable (bit 20)
  969. DIMM Data/Control Output Driving Selection (bit12 - bit15)
  970. Refresh Enable (bit 17)
  971. */
  972. data = 0x022259F1;
  973. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  974. readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  975. /* Turn on for ECC */
  976. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  977. PDC_DIMM_SPD_TYPE, &spd0);
  978. if (spd0 == 0x02) {
  979. data |= (0x01 << 16);
  980. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  981. readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  982. printk(KERN_ERR "Local DIMM ECC Enabled\n");
  983. }
  984. /* DIMM Initialization Select/Enable (bit 18/19) */
  985. data &= (~(1<<18));
  986. data |= (1<<19);
  987. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  988. error = 1;
  989. for (i = 1; i <= 10; i++) { /* polling ~5 secs */
  990. data = readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  991. if (!(data & (1<<19))) {
  992. error = 0;
  993. break;
  994. }
  995. msleep(i*100);
  996. }
  997. return error;
  998. }
  999. static unsigned int pdc20621_dimm_init(struct ata_probe_ent *pe)
  1000. {
  1001. int speed, size, length;
  1002. u32 addr,spd0,pci_status;
  1003. u32 tmp=0;
  1004. u32 time_period=0;
  1005. u32 tcount=0;
  1006. u32 ticks=0;
  1007. u32 clock=0;
  1008. u32 fparam=0;
  1009. void __iomem *mmio = pe->mmio_base;
  1010. /* hard-code chip #0 */
  1011. mmio += PDC_CHIP0_OFS;
  1012. /* Initialize PLL based upon PCI Bus Frequency */
  1013. /* Initialize Time Period Register */
  1014. writel(0xffffffff, mmio + PDC_TIME_PERIOD);
  1015. time_period = readl(mmio + PDC_TIME_PERIOD);
  1016. VPRINTK("Time Period Register (0x40): 0x%x\n", time_period);
  1017. /* Enable timer */
  1018. writel(0x00001a0, mmio + PDC_TIME_CONTROL);
  1019. readl(mmio + PDC_TIME_CONTROL);
  1020. /* Wait 3 seconds */
  1021. msleep(3000);
  1022. /*
  1023. When timer is enabled, counter is decreased every internal
  1024. clock cycle.
  1025. */
  1026. tcount = readl(mmio + PDC_TIME_COUNTER);
  1027. VPRINTK("Time Counter Register (0x44): 0x%x\n", tcount);
  1028. /*
  1029. If SX4 is on PCI-X bus, after 3 seconds, the timer counter
  1030. register should be >= (0xffffffff - 3x10^8).
  1031. */
  1032. if(tcount >= PCI_X_TCOUNT) {
  1033. ticks = (time_period - tcount);
  1034. VPRINTK("Num counters 0x%x (%d)\n", ticks, ticks);
  1035. clock = (ticks / 300000);
  1036. VPRINTK("10 * Internal clk = 0x%x (%d)\n", clock, clock);
  1037. clock = (clock * 33);
  1038. VPRINTK("10 * Internal clk * 33 = 0x%x (%d)\n", clock, clock);
  1039. /* PLL F Param (bit 22:16) */
  1040. fparam = (1400000 / clock) - 2;
  1041. VPRINTK("PLL F Param: 0x%x (%d)\n", fparam, fparam);
  1042. /* OD param = 0x2 (bit 31:30), R param = 0x5 (bit 29:25) */
  1043. pci_status = (0x8a001824 | (fparam << 16));
  1044. } else
  1045. pci_status = PCI_PLL_INIT;
  1046. /* Initialize PLL. */
  1047. VPRINTK("pci_status: 0x%x\n", pci_status);
  1048. writel(pci_status, mmio + PDC_CTL_STATUS);
  1049. readl(mmio + PDC_CTL_STATUS);
  1050. /*
  1051. Read SPD of DIMM by I2C interface,
  1052. and program the DIMM Module Controller.
  1053. */
  1054. if (!(speed = pdc20621_detect_dimm(pe))) {
  1055. printk(KERN_ERR "Detect Local DIMM Fail\n");
  1056. return 1; /* DIMM error */
  1057. }
  1058. VPRINTK("Local DIMM Speed = %d\n", speed);
  1059. /* Programming DIMM0 Module Control Register (index_CID0:80h) */
  1060. size = pdc20621_prog_dimm0(pe);
  1061. VPRINTK("Local DIMM Size = %dMB\n",size);
  1062. /* Programming DIMM Module Global Control Register (index_CID0:88h) */
  1063. if (pdc20621_prog_dimm_global(pe)) {
  1064. printk(KERN_ERR "Programming DIMM Module Global Control Register Fail\n");
  1065. return 1;
  1066. }
  1067. #ifdef ATA_VERBOSE_DEBUG
  1068. {
  1069. u8 test_parttern1[40] = {0x55,0xAA,'P','r','o','m','i','s','e',' ',
  1070. 'N','o','t',' ','Y','e','t',' ','D','e','f','i','n','e','d',' ',
  1071. '1','.','1','0',
  1072. '9','8','0','3','1','6','1','2',0,0};
  1073. u8 test_parttern2[40] = {0};
  1074. pdc20621_put_to_dimm(pe, (void *) test_parttern2, 0x10040, 40);
  1075. pdc20621_put_to_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1076. pdc20621_put_to_dimm(pe, (void *) test_parttern1, 0x10040, 40);
  1077. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1078. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1079. test_parttern2[1], &(test_parttern2[2]));
  1080. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x10040,
  1081. 40);
  1082. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1083. test_parttern2[1], &(test_parttern2[2]));
  1084. pdc20621_put_to_dimm(pe, (void *) test_parttern1, 0x40, 40);
  1085. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1086. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1087. test_parttern2[1], &(test_parttern2[2]));
  1088. }
  1089. #endif
  1090. /* ECC initiliazation. */
  1091. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  1092. PDC_DIMM_SPD_TYPE, &spd0);
  1093. if (spd0 == 0x02) {
  1094. VPRINTK("Start ECC initialization\n");
  1095. addr = 0;
  1096. length = size * 1024 * 1024;
  1097. while (addr < length) {
  1098. pdc20621_put_to_dimm(pe, (void *) &tmp, addr,
  1099. sizeof(u32));
  1100. addr += sizeof(u32);
  1101. }
  1102. VPRINTK("Finish ECC initialization\n");
  1103. }
  1104. return 0;
  1105. }
  1106. static void pdc_20621_init(struct ata_probe_ent *pe)
  1107. {
  1108. u32 tmp;
  1109. void __iomem *mmio = pe->mmio_base;
  1110. /* hard-code chip #0 */
  1111. mmio += PDC_CHIP0_OFS;
  1112. /*
  1113. * Select page 0x40 for our 32k DIMM window
  1114. */
  1115. tmp = readl(mmio + PDC_20621_DIMM_WINDOW) & 0xffff0000;
  1116. tmp |= PDC_PAGE_WINDOW; /* page 40h; arbitrarily selected */
  1117. writel(tmp, mmio + PDC_20621_DIMM_WINDOW);
  1118. /*
  1119. * Reset Host DMA
  1120. */
  1121. tmp = readl(mmio + PDC_HDMA_CTLSTAT);
  1122. tmp |= PDC_RESET;
  1123. writel(tmp, mmio + PDC_HDMA_CTLSTAT);
  1124. readl(mmio + PDC_HDMA_CTLSTAT); /* flush */
  1125. udelay(10);
  1126. tmp = readl(mmio + PDC_HDMA_CTLSTAT);
  1127. tmp &= ~PDC_RESET;
  1128. writel(tmp, mmio + PDC_HDMA_CTLSTAT);
  1129. readl(mmio + PDC_HDMA_CTLSTAT); /* flush */
  1130. }
  1131. static int pdc_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1132. {
  1133. static int printed_version;
  1134. struct ata_probe_ent *probe_ent = NULL;
  1135. unsigned long base;
  1136. void __iomem *mmio_base;
  1137. void __iomem *dimm_mmio = NULL;
  1138. struct pdc_host_priv *hpriv = NULL;
  1139. unsigned int board_idx = (unsigned int) ent->driver_data;
  1140. int pci_dev_busy = 0;
  1141. int rc;
  1142. if (!printed_version++)
  1143. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1144. /*
  1145. * If this driver happens to only be useful on Apple's K2, then
  1146. * we should check that here as it has a normal Serverworks ID
  1147. */
  1148. rc = pci_enable_device(pdev);
  1149. if (rc)
  1150. return rc;
  1151. rc = pci_request_regions(pdev, DRV_NAME);
  1152. if (rc) {
  1153. pci_dev_busy = 1;
  1154. goto err_out;
  1155. }
  1156. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  1157. if (rc)
  1158. goto err_out_regions;
  1159. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  1160. if (rc)
  1161. goto err_out_regions;
  1162. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1163. if (probe_ent == NULL) {
  1164. rc = -ENOMEM;
  1165. goto err_out_regions;
  1166. }
  1167. memset(probe_ent, 0, sizeof(*probe_ent));
  1168. probe_ent->dev = pci_dev_to_dev(pdev);
  1169. INIT_LIST_HEAD(&probe_ent->node);
  1170. mmio_base = pci_iomap(pdev, 3, 0);
  1171. if (mmio_base == NULL) {
  1172. rc = -ENOMEM;
  1173. goto err_out_free_ent;
  1174. }
  1175. base = (unsigned long) mmio_base;
  1176. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1177. if (!hpriv) {
  1178. rc = -ENOMEM;
  1179. goto err_out_iounmap;
  1180. }
  1181. memset(hpriv, 0, sizeof(*hpriv));
  1182. dimm_mmio = pci_iomap(pdev, 4, 0);
  1183. if (!dimm_mmio) {
  1184. kfree(hpriv);
  1185. rc = -ENOMEM;
  1186. goto err_out_iounmap;
  1187. }
  1188. hpriv->dimm_mmio = dimm_mmio;
  1189. probe_ent->sht = pdc_port_info[board_idx].sht;
  1190. probe_ent->host_flags = pdc_port_info[board_idx].host_flags;
  1191. probe_ent->pio_mask = pdc_port_info[board_idx].pio_mask;
  1192. probe_ent->mwdma_mask = pdc_port_info[board_idx].mwdma_mask;
  1193. probe_ent->udma_mask = pdc_port_info[board_idx].udma_mask;
  1194. probe_ent->port_ops = pdc_port_info[board_idx].port_ops;
  1195. probe_ent->irq = pdev->irq;
  1196. probe_ent->irq_flags = SA_SHIRQ;
  1197. probe_ent->mmio_base = mmio_base;
  1198. probe_ent->private_data = hpriv;
  1199. base += PDC_CHIP0_OFS;
  1200. probe_ent->n_ports = 4;
  1201. pdc_sata_setup_port(&probe_ent->port[0], base + 0x200);
  1202. pdc_sata_setup_port(&probe_ent->port[1], base + 0x280);
  1203. pdc_sata_setup_port(&probe_ent->port[2], base + 0x300);
  1204. pdc_sata_setup_port(&probe_ent->port[3], base + 0x380);
  1205. pci_set_master(pdev);
  1206. /* initialize adapter */
  1207. /* initialize local dimm */
  1208. if (pdc20621_dimm_init(probe_ent)) {
  1209. rc = -ENOMEM;
  1210. goto err_out_iounmap_dimm;
  1211. }
  1212. pdc_20621_init(probe_ent);
  1213. /* FIXME: check ata_device_add return value */
  1214. ata_device_add(probe_ent);
  1215. kfree(probe_ent);
  1216. return 0;
  1217. err_out_iounmap_dimm: /* only get to this label if 20621 */
  1218. kfree(hpriv);
  1219. pci_iounmap(pdev, dimm_mmio);
  1220. err_out_iounmap:
  1221. pci_iounmap(pdev, mmio_base);
  1222. err_out_free_ent:
  1223. kfree(probe_ent);
  1224. err_out_regions:
  1225. pci_release_regions(pdev);
  1226. err_out:
  1227. if (!pci_dev_busy)
  1228. pci_disable_device(pdev);
  1229. return rc;
  1230. }
  1231. static int __init pdc_sata_init(void)
  1232. {
  1233. return pci_module_init(&pdc_sata_pci_driver);
  1234. }
  1235. static void __exit pdc_sata_exit(void)
  1236. {
  1237. pci_unregister_driver(&pdc_sata_pci_driver);
  1238. }
  1239. MODULE_AUTHOR("Jeff Garzik");
  1240. MODULE_DESCRIPTION("Promise SATA low-level driver");
  1241. MODULE_LICENSE("GPL");
  1242. MODULE_DEVICE_TABLE(pci, pdc_sata_pci_tbl);
  1243. MODULE_VERSION(DRV_VERSION);
  1244. module_init(pdc_sata_init);
  1245. module_exit(pdc_sata_exit);