cafe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
  3. *
  4. * Copyright © 2006 Red Hat, Inc.
  5. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  6. */
  7. #define DEBUG
  8. #include <linux/device.h>
  9. #undef DEBUG
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/nand.h>
  12. #include <linux/rslib.h>
  13. #include <linux/pci.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/dma-mapping.h>
  17. #include <asm/io.h>
  18. #define CAFE_NAND_CTRL1 0x00
  19. #define CAFE_NAND_CTRL2 0x04
  20. #define CAFE_NAND_CTRL3 0x08
  21. #define CAFE_NAND_STATUS 0x0c
  22. #define CAFE_NAND_IRQ 0x10
  23. #define CAFE_NAND_IRQ_MASK 0x14
  24. #define CAFE_NAND_DATA_LEN 0x18
  25. #define CAFE_NAND_ADDR1 0x1c
  26. #define CAFE_NAND_ADDR2 0x20
  27. #define CAFE_NAND_TIMING1 0x24
  28. #define CAFE_NAND_TIMING2 0x28
  29. #define CAFE_NAND_TIMING3 0x2c
  30. #define CAFE_NAND_NONMEM 0x30
  31. #define CAFE_NAND_ECC_RESULT 0x3C
  32. #define CAFE_NAND_DMA_CTRL 0x40
  33. #define CAFE_NAND_DMA_ADDR0 0x44
  34. #define CAFE_NAND_DMA_ADDR1 0x48
  35. #define CAFE_NAND_ECC_SYN01 0x50
  36. #define CAFE_NAND_ECC_SYN23 0x54
  37. #define CAFE_NAND_ECC_SYN45 0x58
  38. #define CAFE_NAND_ECC_SYN67 0x5c
  39. #define CAFE_NAND_READ_DATA 0x1000
  40. #define CAFE_NAND_WRITE_DATA 0x2000
  41. #define CAFE_GLOBAL_CTRL 0x3004
  42. #define CAFE_GLOBAL_IRQ 0x3008
  43. #define CAFE_GLOBAL_IRQ_MASK 0x300c
  44. #define CAFE_NAND_RESET 0x3034
  45. struct cafe_priv {
  46. struct nand_chip nand;
  47. struct pci_dev *pdev;
  48. void __iomem *mmio;
  49. struct rs_control *rs;
  50. uint32_t ctl1;
  51. uint32_t ctl2;
  52. int datalen;
  53. int nr_data;
  54. int data_pos;
  55. int page_addr;
  56. dma_addr_t dmaaddr;
  57. unsigned char *dmabuf;
  58. };
  59. static int usedma = 1;
  60. module_param(usedma, int, 0644);
  61. static int skipbbt = 0;
  62. module_param(skipbbt, int, 0644);
  63. static int debug = 0;
  64. module_param(debug, int, 0644);
  65. static int regdebug = 0;
  66. module_param(regdebug, int, 0644);
  67. static int checkecc = 1;
  68. module_param(checkecc, int, 0644);
  69. static int numtimings;
  70. static int timing[3];
  71. module_param_array(timing, int, &numtimings, 0644);
  72. /* Hrm. Why isn't this already conditional on something in the struct device? */
  73. #define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
  74. /* Make it easier to switch to PIO if we need to */
  75. #define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
  76. #define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
  77. static int cafe_device_ready(struct mtd_info *mtd)
  78. {
  79. struct cafe_priv *cafe = mtd->priv;
  80. int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
  81. uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
  82. cafe_writel(cafe, irqs, NAND_IRQ);
  83. cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
  84. result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
  85. cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  86. return result;
  87. }
  88. static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  89. {
  90. struct cafe_priv *cafe = mtd->priv;
  91. if (usedma)
  92. memcpy(cafe->dmabuf + cafe->datalen, buf, len);
  93. else
  94. memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
  95. cafe->datalen += len;
  96. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
  97. len, cafe->datalen);
  98. }
  99. static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  100. {
  101. struct cafe_priv *cafe = mtd->priv;
  102. if (usedma)
  103. memcpy(buf, cafe->dmabuf + cafe->datalen, len);
  104. else
  105. memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
  106. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
  107. len, cafe->datalen);
  108. cafe->datalen += len;
  109. }
  110. static uint8_t cafe_read_byte(struct mtd_info *mtd)
  111. {
  112. struct cafe_priv *cafe = mtd->priv;
  113. uint8_t d;
  114. cafe_read_buf(mtd, &d, 1);
  115. cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
  116. return d;
  117. }
  118. static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  119. int column, int page_addr)
  120. {
  121. struct cafe_priv *cafe = mtd->priv;
  122. int adrbytes = 0;
  123. uint32_t ctl1;
  124. uint32_t doneint = 0x80000000;
  125. cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
  126. command, column, page_addr);
  127. if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
  128. /* Second half of a command we already calculated */
  129. cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
  130. ctl1 = cafe->ctl1;
  131. cafe->ctl2 &= ~(1<<30);
  132. cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
  133. cafe->ctl1, cafe->nr_data);
  134. goto do_command;
  135. }
  136. /* Reset ECC engine */
  137. cafe_writel(cafe, 0, NAND_CTRL2);
  138. /* Emulate NAND_CMD_READOOB on large-page chips */
  139. if (mtd->writesize > 512 &&
  140. command == NAND_CMD_READOOB) {
  141. column += mtd->writesize;
  142. command = NAND_CMD_READ0;
  143. }
  144. /* FIXME: Do we need to send read command before sending data
  145. for small-page chips, to position the buffer correctly? */
  146. if (column != -1) {
  147. cafe_writel(cafe, column, NAND_ADDR1);
  148. adrbytes = 2;
  149. if (page_addr != -1)
  150. goto write_adr2;
  151. } else if (page_addr != -1) {
  152. cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
  153. page_addr >>= 16;
  154. write_adr2:
  155. cafe_writel(cafe, page_addr, NAND_ADDR2);
  156. adrbytes += 2;
  157. if (mtd->size > mtd->writesize << 16)
  158. adrbytes++;
  159. }
  160. cafe->data_pos = cafe->datalen = 0;
  161. /* Set command valid bit */
  162. ctl1 = 0x80000000 | command;
  163. /* Set RD or WR bits as appropriate */
  164. if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
  165. ctl1 |= (1<<26); /* rd */
  166. /* Always 5 bytes, for now */
  167. cafe->datalen = 4;
  168. /* And one address cycle -- even for STATUS, since the controller doesn't work without */
  169. adrbytes = 1;
  170. } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
  171. command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
  172. ctl1 |= 1<<26; /* rd */
  173. /* For now, assume just read to end of page */
  174. cafe->datalen = mtd->writesize + mtd->oobsize - column;
  175. } else if (command == NAND_CMD_SEQIN)
  176. ctl1 |= 1<<25; /* wr */
  177. /* Set number of address bytes */
  178. if (adrbytes)
  179. ctl1 |= ((adrbytes-1)|8) << 27;
  180. if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
  181. /* Ignore the first command of a pair; the hardware
  182. deals with them both at once, later */
  183. cafe->ctl1 = ctl1;
  184. cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
  185. cafe->ctl1, cafe->datalen);
  186. return;
  187. }
  188. /* RNDOUT and READ0 commands need a following byte */
  189. if (command == NAND_CMD_RNDOUT)
  190. cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
  191. else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
  192. cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
  193. do_command:
  194. cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
  195. cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
  196. /* NB: The datasheet lies -- we really should be subtracting 1 here */
  197. cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
  198. cafe_writel(cafe, 0x90000000, NAND_IRQ);
  199. if (usedma && (ctl1 & (3<<25))) {
  200. uint32_t dmactl = 0xc0000000 + cafe->datalen;
  201. /* If WR or RD bits set, set up DMA */
  202. if (ctl1 & (1<<26)) {
  203. /* It's a read */
  204. dmactl |= (1<<29);
  205. /* ... so it's done when the DMA is done, not just
  206. the command. */
  207. doneint = 0x10000000;
  208. }
  209. cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
  210. }
  211. cafe->datalen = 0;
  212. if (unlikely(regdebug)) {
  213. int i;
  214. printk("About to write command %08x to register 0\n", ctl1);
  215. for (i=4; i< 0x5c; i+=4)
  216. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  217. }
  218. cafe_writel(cafe, ctl1, NAND_CTRL1);
  219. /* Apply this short delay always to ensure that we do wait tWB in
  220. * any case on any machine. */
  221. ndelay(100);
  222. if (1) {
  223. int c;
  224. uint32_t irqs;
  225. for (c = 500000; c != 0; c--) {
  226. irqs = cafe_readl(cafe, NAND_IRQ);
  227. if (irqs & doneint)
  228. break;
  229. udelay(1);
  230. if (!(c % 100000))
  231. cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
  232. cpu_relax();
  233. }
  234. cafe_writel(cafe, doneint, NAND_IRQ);
  235. cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
  236. command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
  237. }
  238. WARN_ON(cafe->ctl2 & (1<<30));
  239. switch (command) {
  240. case NAND_CMD_CACHEDPROG:
  241. case NAND_CMD_PAGEPROG:
  242. case NAND_CMD_ERASE1:
  243. case NAND_CMD_ERASE2:
  244. case NAND_CMD_SEQIN:
  245. case NAND_CMD_RNDIN:
  246. case NAND_CMD_STATUS:
  247. case NAND_CMD_DEPLETE1:
  248. case NAND_CMD_RNDOUT:
  249. case NAND_CMD_STATUS_ERROR:
  250. case NAND_CMD_STATUS_ERROR0:
  251. case NAND_CMD_STATUS_ERROR1:
  252. case NAND_CMD_STATUS_ERROR2:
  253. case NAND_CMD_STATUS_ERROR3:
  254. cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
  255. return;
  256. }
  257. nand_wait_ready(mtd);
  258. cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
  259. }
  260. static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
  261. {
  262. //struct cafe_priv *cafe = mtd->priv;
  263. // cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
  264. }
  265. static int cafe_nand_interrupt(int irq, void *id)
  266. {
  267. struct mtd_info *mtd = id;
  268. struct cafe_priv *cafe = mtd->priv;
  269. uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
  270. cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
  271. if (!irqs)
  272. return IRQ_NONE;
  273. cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
  274. return IRQ_HANDLED;
  275. }
  276. static void cafe_nand_bug(struct mtd_info *mtd)
  277. {
  278. BUG();
  279. }
  280. static int cafe_nand_write_oob(struct mtd_info *mtd,
  281. struct nand_chip *chip, int page)
  282. {
  283. int status = 0;
  284. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  285. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  286. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  287. status = chip->waitfunc(mtd, chip);
  288. return status & NAND_STATUS_FAIL ? -EIO : 0;
  289. }
  290. /* Don't use -- use nand_read_oob_std for now */
  291. static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  292. int page, int sndcmd)
  293. {
  294. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  295. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  296. return 1;
  297. }
  298. /**
  299. * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
  300. * @mtd: mtd info structure
  301. * @chip: nand chip info structure
  302. * @buf: buffer to store read data
  303. *
  304. * The hw generator calculates the error syndrome automatically. Therefor
  305. * we need a special oob layout and handling.
  306. */
  307. static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  308. uint8_t *buf)
  309. {
  310. struct cafe_priv *cafe = mtd->priv;
  311. cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
  312. cafe_readl(cafe, NAND_ECC_RESULT),
  313. cafe_readl(cafe, NAND_ECC_SYN01));
  314. chip->read_buf(mtd, buf, mtd->writesize);
  315. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  316. if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
  317. unsigned short syn[8], pat[4];
  318. int pos[4];
  319. u8 *oob = chip->oob_poi;
  320. int i, n;
  321. for (i=0; i<8; i+=2) {
  322. uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
  323. syn[i] = cafe->rs->index_of[tmp & 0xfff];
  324. syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff];
  325. }
  326. n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
  327. pat);
  328. for (i = 0; i < n; i++) {
  329. int p = pos[i];
  330. /* The 12-bit symbols are mapped to bytes here */
  331. if (p > 1374) {
  332. /* out of range */
  333. n = -1374;
  334. } else if (p == 0) {
  335. /* high four bits do not correspond to data */
  336. if (pat[i] > 0xff)
  337. n = -2048;
  338. else
  339. buf[0] ^= pat[i];
  340. } else if (p == 1365) {
  341. buf[2047] ^= pat[i] >> 4;
  342. oob[0] ^= pat[i] << 4;
  343. } else if (p > 1365) {
  344. if ((p & 1) == 1) {
  345. oob[3*p/2 - 2048] ^= pat[i] >> 4;
  346. oob[3*p/2 - 2047] ^= pat[i] << 4;
  347. } else {
  348. oob[3*p/2 - 2049] ^= pat[i] >> 8;
  349. oob[3*p/2 - 2048] ^= pat[i];
  350. }
  351. } else if ((p & 1) == 1) {
  352. buf[3*p/2] ^= pat[i] >> 4;
  353. buf[3*p/2 + 1] ^= pat[i] << 4;
  354. } else {
  355. buf[3*p/2 - 1] ^= pat[i] >> 8;
  356. buf[3*p/2] ^= pat[i];
  357. }
  358. }
  359. if (n < 0) {
  360. dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
  361. cafe_readl(cafe, NAND_ADDR2) * 2048);
  362. for (i = 0; i < 0x5c; i += 4)
  363. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  364. mtd->ecc_stats.failed++;
  365. } else {
  366. dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
  367. mtd->ecc_stats.corrected += n;
  368. }
  369. }
  370. return 0;
  371. }
  372. static struct nand_ecclayout cafe_oobinfo_2048 = {
  373. .eccbytes = 14,
  374. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  375. .oobfree = {{14, 50}}
  376. };
  377. /* Ick. The BBT code really ought to be able to work this bit out
  378. for itself from the above, at least for the 2KiB case */
  379. static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
  380. static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
  381. static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
  382. static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
  383. static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
  384. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  385. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  386. .offs = 14,
  387. .len = 4,
  388. .veroffs = 18,
  389. .maxblocks = 4,
  390. .pattern = cafe_bbt_pattern_2048
  391. };
  392. static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
  393. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  394. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  395. .offs = 14,
  396. .len = 4,
  397. .veroffs = 18,
  398. .maxblocks = 4,
  399. .pattern = cafe_mirror_pattern_2048
  400. };
  401. static struct nand_ecclayout cafe_oobinfo_512 = {
  402. .eccbytes = 14,
  403. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  404. .oobfree = {{14, 2}}
  405. };
  406. static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
  407. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  408. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  409. .offs = 14,
  410. .len = 1,
  411. .veroffs = 15,
  412. .maxblocks = 4,
  413. .pattern = cafe_bbt_pattern_512
  414. };
  415. static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
  416. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  417. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  418. .offs = 14,
  419. .len = 1,
  420. .veroffs = 15,
  421. .maxblocks = 4,
  422. .pattern = cafe_mirror_pattern_512
  423. };
  424. static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
  425. struct nand_chip *chip, const uint8_t *buf)
  426. {
  427. struct cafe_priv *cafe = mtd->priv;
  428. chip->write_buf(mtd, buf, mtd->writesize);
  429. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  430. /* Set up ECC autogeneration */
  431. cafe->ctl2 |= (1<<30);
  432. }
  433. static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  434. const uint8_t *buf, int page, int cached, int raw)
  435. {
  436. int status;
  437. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  438. if (unlikely(raw))
  439. chip->ecc.write_page_raw(mtd, chip, buf);
  440. else
  441. chip->ecc.write_page(mtd, chip, buf);
  442. /*
  443. * Cached progamming disabled for now, Not sure if its worth the
  444. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  445. */
  446. cached = 0;
  447. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  448. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  449. status = chip->waitfunc(mtd, chip);
  450. /*
  451. * See if operation failed and additional status checks are
  452. * available
  453. */
  454. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  455. status = chip->errstat(mtd, chip, FL_WRITING, status,
  456. page);
  457. if (status & NAND_STATUS_FAIL)
  458. return -EIO;
  459. } else {
  460. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  461. status = chip->waitfunc(mtd, chip);
  462. }
  463. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  464. /* Send command to read back the data */
  465. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  466. if (chip->verify_buf(mtd, buf, mtd->writesize))
  467. return -EIO;
  468. #endif
  469. return 0;
  470. }
  471. static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  472. {
  473. return 0;
  474. }
  475. /* F_2[X]/(X**6+X+1) */
  476. static unsigned short __devinit gf64_mul(u8 a, u8 b)
  477. {
  478. u8 c;
  479. unsigned int i;
  480. c = 0;
  481. for (i = 0; i < 6; i++) {
  482. if (a & 1)
  483. c ^= b;
  484. a >>= 1;
  485. b <<= 1;
  486. if ((b & 0x40) != 0)
  487. b ^= 0x43;
  488. }
  489. return c;
  490. }
  491. /* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
  492. static u16 __devinit gf4096_mul(u16 a, u16 b)
  493. {
  494. u8 ah, al, bh, bl, ch, cl;
  495. ah = a >> 6;
  496. al = a & 0x3f;
  497. bh = b >> 6;
  498. bl = b & 0x3f;
  499. ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
  500. cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
  501. return (ch << 6) ^ cl;
  502. }
  503. static int __devinit cafe_mul(int x)
  504. {
  505. if (x == 0)
  506. return 1;
  507. return gf4096_mul(x, 0xe01);
  508. }
  509. static int __devinit cafe_nand_probe(struct pci_dev *pdev,
  510. const struct pci_device_id *ent)
  511. {
  512. struct mtd_info *mtd;
  513. struct cafe_priv *cafe;
  514. uint32_t ctrl;
  515. int err = 0;
  516. err = pci_enable_device(pdev);
  517. if (err)
  518. return err;
  519. pci_set_master(pdev);
  520. mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
  521. if (!mtd) {
  522. dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
  523. return -ENOMEM;
  524. }
  525. cafe = (void *)(&mtd[1]);
  526. mtd->priv = cafe;
  527. mtd->owner = THIS_MODULE;
  528. cafe->pdev = pdev;
  529. cafe->mmio = pci_iomap(pdev, 0, 0);
  530. if (!cafe->mmio) {
  531. dev_warn(&pdev->dev, "failed to iomap\n");
  532. err = -ENOMEM;
  533. goto out_free_mtd;
  534. }
  535. cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
  536. &cafe->dmaaddr, GFP_KERNEL);
  537. if (!cafe->dmabuf) {
  538. err = -ENOMEM;
  539. goto out_ior;
  540. }
  541. cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
  542. cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
  543. if (!cafe->rs) {
  544. err = -ENOMEM;
  545. goto out_ior;
  546. }
  547. cafe->nand.cmdfunc = cafe_nand_cmdfunc;
  548. cafe->nand.dev_ready = cafe_device_ready;
  549. cafe->nand.read_byte = cafe_read_byte;
  550. cafe->nand.read_buf = cafe_read_buf;
  551. cafe->nand.write_buf = cafe_write_buf;
  552. cafe->nand.select_chip = cafe_select_chip;
  553. cafe->nand.chip_delay = 0;
  554. /* Enable the following for a flash based bad block table */
  555. cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
  556. if (skipbbt) {
  557. cafe->nand.options |= NAND_SKIP_BBTSCAN;
  558. cafe->nand.block_bad = cafe_nand_block_bad;
  559. }
  560. if (numtimings && numtimings != 3) {
  561. dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
  562. }
  563. if (numtimings == 3) {
  564. cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
  565. timing[0], timing[1], timing[2]);
  566. } else {
  567. timing[0] = cafe_readl(cafe, NAND_TIMING1);
  568. timing[1] = cafe_readl(cafe, NAND_TIMING2);
  569. timing[2] = cafe_readl(cafe, NAND_TIMING3);
  570. if (timing[0] | timing[1] | timing[2]) {
  571. cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
  572. timing[0], timing[1], timing[2]);
  573. } else {
  574. dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
  575. timing[0] = timing[1] = timing[2] = 0xffffffff;
  576. }
  577. }
  578. /* Start off by resetting the NAND controller completely */
  579. cafe_writel(cafe, 1, NAND_RESET);
  580. cafe_writel(cafe, 0, NAND_RESET);
  581. cafe_writel(cafe, timing[0], NAND_TIMING1);
  582. cafe_writel(cafe, timing[1], NAND_TIMING2);
  583. cafe_writel(cafe, timing[2], NAND_TIMING3);
  584. cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
  585. err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
  586. "CAFE NAND", mtd);
  587. if (err) {
  588. dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
  589. goto out_free_dma;
  590. }
  591. /* Disable master reset, enable NAND clock */
  592. ctrl = cafe_readl(cafe, GLOBAL_CTRL);
  593. ctrl &= 0xffffeff0;
  594. ctrl |= 0x00007000;
  595. cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
  596. cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
  597. cafe_writel(cafe, 0, NAND_DMA_CTRL);
  598. cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
  599. cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
  600. /* Set up DMA address */
  601. cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
  602. if (sizeof(cafe->dmaaddr) > 4)
  603. /* Shift in two parts to shut the compiler up */
  604. cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
  605. else
  606. cafe_writel(cafe, 0, NAND_DMA_ADDR1);
  607. cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
  608. cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
  609. /* Enable NAND IRQ in global IRQ mask register */
  610. cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
  611. cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
  612. cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  613. /* Scan to find existence of the device */
  614. if (nand_scan_ident(mtd, 1)) {
  615. err = -ENXIO;
  616. goto out_irq;
  617. }
  618. cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
  619. if (mtd->writesize == 2048)
  620. cafe->ctl2 |= 1<<29; /* 2KiB page size */
  621. /* Set up ECC according to the type of chip we found */
  622. if (mtd->writesize == 2048) {
  623. cafe->nand.ecc.layout = &cafe_oobinfo_2048;
  624. cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
  625. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
  626. } else if (mtd->writesize == 512) {
  627. cafe->nand.ecc.layout = &cafe_oobinfo_512;
  628. cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
  629. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
  630. } else {
  631. printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
  632. mtd->writesize);
  633. goto out_irq;
  634. }
  635. cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  636. cafe->nand.ecc.size = mtd->writesize;
  637. cafe->nand.ecc.bytes = 14;
  638. cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
  639. cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
  640. cafe->nand.ecc.correct = (void *)cafe_nand_bug;
  641. cafe->nand.write_page = cafe_nand_write_page;
  642. cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
  643. cafe->nand.ecc.write_oob = cafe_nand_write_oob;
  644. cafe->nand.ecc.read_page = cafe_nand_read_page;
  645. cafe->nand.ecc.read_oob = cafe_nand_read_oob;
  646. err = nand_scan_tail(mtd);
  647. if (err)
  648. goto out_irq;
  649. pci_set_drvdata(pdev, mtd);
  650. add_mtd_device(mtd);
  651. goto out;
  652. out_irq:
  653. /* Disable NAND IRQ in global IRQ mask register */
  654. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  655. free_irq(pdev->irq, mtd);
  656. out_free_dma:
  657. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  658. out_ior:
  659. pci_iounmap(pdev, cafe->mmio);
  660. out_free_mtd:
  661. kfree(mtd);
  662. out:
  663. return err;
  664. }
  665. static void __devexit cafe_nand_remove(struct pci_dev *pdev)
  666. {
  667. struct mtd_info *mtd = pci_get_drvdata(pdev);
  668. struct cafe_priv *cafe = mtd->priv;
  669. del_mtd_device(mtd);
  670. /* Disable NAND IRQ in global IRQ mask register */
  671. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  672. free_irq(pdev->irq, mtd);
  673. nand_release(mtd);
  674. free_rs(cafe->rs);
  675. pci_iounmap(pdev, cafe->mmio);
  676. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  677. kfree(mtd);
  678. }
  679. static struct pci_device_id cafe_nand_tbl[] = {
  680. { 0x11ab, 0x4100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MEMORY_FLASH << 8, 0xFFFF0 }
  681. };
  682. MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
  683. static struct pci_driver cafe_nand_pci_driver = {
  684. .name = "CAFÉ NAND",
  685. .id_table = cafe_nand_tbl,
  686. .probe = cafe_nand_probe,
  687. .remove = __devexit_p(cafe_nand_remove),
  688. #ifdef CONFIG_PMx
  689. .suspend = cafe_nand_suspend,
  690. .resume = cafe_nand_resume,
  691. #endif
  692. };
  693. static int cafe_nand_init(void)
  694. {
  695. return pci_register_driver(&cafe_nand_pci_driver);
  696. }
  697. static void cafe_nand_exit(void)
  698. {
  699. pci_unregister_driver(&cafe_nand_pci_driver);
  700. }
  701. module_init(cafe_nand_init);
  702. module_exit(cafe_nand_exit);
  703. MODULE_LICENSE("GPL");
  704. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  705. MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");