cafe_nand.c 25 KB

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