omap2.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * linux/drivers/mtd/onenand/omap2.c
  3. *
  4. * OneNAND driver for OMAP2 / OMAP3
  5. *
  6. * Copyright © 2005-2006 Nokia Corporation
  7. *
  8. * Author: Jarkko Lavinen <jarkko.lavinen@nokia.com> and Juha Yrjölä
  9. * IRQ and DMA support written by Timo Teras
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published by
  13. * the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program; see the file COPYING. If not, write to the Free Software
  22. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. */
  25. #include <linux/device.h>
  26. #include <linux/module.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/onenand.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/delay.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/io.h>
  35. #include <linux/slab.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <linux/gpio.h>
  38. #include <asm/mach/flash.h>
  39. #include <linux/platform_data/mtd-onenand-omap2.h>
  40. #include <linux/omap-dma.h>
  41. #define DRIVER_NAME "omap2-onenand"
  42. #define ONENAND_BUFRAM_SIZE (1024 * 5)
  43. struct omap2_onenand {
  44. struct platform_device *pdev;
  45. int gpmc_cs;
  46. unsigned long phys_base;
  47. unsigned int mem_size;
  48. int gpio_irq;
  49. struct mtd_info mtd;
  50. struct onenand_chip onenand;
  51. struct completion irq_done;
  52. struct completion dma_done;
  53. int dma_channel;
  54. int freq;
  55. int (*setup)(void __iomem *base, int *freq_ptr);
  56. struct regulator *regulator;
  57. u8 flags;
  58. };
  59. static void omap2_onenand_dma_cb(int lch, u16 ch_status, void *data)
  60. {
  61. struct omap2_onenand *c = data;
  62. complete(&c->dma_done);
  63. }
  64. static irqreturn_t omap2_onenand_interrupt(int irq, void *dev_id)
  65. {
  66. struct omap2_onenand *c = dev_id;
  67. complete(&c->irq_done);
  68. return IRQ_HANDLED;
  69. }
  70. static inline unsigned short read_reg(struct omap2_onenand *c, int reg)
  71. {
  72. return readw(c->onenand.base + reg);
  73. }
  74. static inline void write_reg(struct omap2_onenand *c, unsigned short value,
  75. int reg)
  76. {
  77. writew(value, c->onenand.base + reg);
  78. }
  79. static void wait_err(char *msg, int state, unsigned int ctrl, unsigned int intr)
  80. {
  81. printk(KERN_ERR "onenand_wait: %s! state %d ctrl 0x%04x intr 0x%04x\n",
  82. msg, state, ctrl, intr);
  83. }
  84. static void wait_warn(char *msg, int state, unsigned int ctrl,
  85. unsigned int intr)
  86. {
  87. printk(KERN_WARNING "onenand_wait: %s! state %d ctrl 0x%04x "
  88. "intr 0x%04x\n", msg, state, ctrl, intr);
  89. }
  90. static int omap2_onenand_wait(struct mtd_info *mtd, int state)
  91. {
  92. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  93. struct onenand_chip *this = mtd->priv;
  94. unsigned int intr = 0;
  95. unsigned int ctrl, ctrl_mask;
  96. unsigned long timeout;
  97. u32 syscfg;
  98. if (state == FL_RESETING || state == FL_PREPARING_ERASE ||
  99. state == FL_VERIFYING_ERASE) {
  100. int i = 21;
  101. unsigned int intr_flags = ONENAND_INT_MASTER;
  102. switch (state) {
  103. case FL_RESETING:
  104. intr_flags |= ONENAND_INT_RESET;
  105. break;
  106. case FL_PREPARING_ERASE:
  107. intr_flags |= ONENAND_INT_ERASE;
  108. break;
  109. case FL_VERIFYING_ERASE:
  110. i = 101;
  111. break;
  112. }
  113. while (--i) {
  114. udelay(1);
  115. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  116. if (intr & ONENAND_INT_MASTER)
  117. break;
  118. }
  119. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  120. if (ctrl & ONENAND_CTRL_ERROR) {
  121. wait_err("controller error", state, ctrl, intr);
  122. return -EIO;
  123. }
  124. if ((intr & intr_flags) == intr_flags)
  125. return 0;
  126. /* Continue in wait for interrupt branch */
  127. }
  128. if (state != FL_READING) {
  129. int result;
  130. /* Turn interrupts on */
  131. syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
  132. if (!(syscfg & ONENAND_SYS_CFG1_IOBE)) {
  133. syscfg |= ONENAND_SYS_CFG1_IOBE;
  134. write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
  135. if (c->flags & ONENAND_IN_OMAP34XX)
  136. /* Add a delay to let GPIO settle */
  137. syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
  138. }
  139. reinit_completion(&c->irq_done);
  140. if (c->gpio_irq) {
  141. result = gpio_get_value(c->gpio_irq);
  142. if (result == -1) {
  143. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  144. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  145. wait_err("gpio error", state, ctrl, intr);
  146. return -EIO;
  147. }
  148. } else
  149. result = 0;
  150. if (result == 0) {
  151. int retry_cnt = 0;
  152. retry:
  153. result = wait_for_completion_timeout(&c->irq_done,
  154. msecs_to_jiffies(20));
  155. if (result == 0) {
  156. /* Timeout after 20ms */
  157. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  158. if (ctrl & ONENAND_CTRL_ONGO &&
  159. !this->ongoing) {
  160. /*
  161. * The operation seems to be still going
  162. * so give it some more time.
  163. */
  164. retry_cnt += 1;
  165. if (retry_cnt < 3)
  166. goto retry;
  167. intr = read_reg(c,
  168. ONENAND_REG_INTERRUPT);
  169. wait_err("timeout", state, ctrl, intr);
  170. return -EIO;
  171. }
  172. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  173. if ((intr & ONENAND_INT_MASTER) == 0)
  174. wait_warn("timeout", state, ctrl, intr);
  175. }
  176. }
  177. } else {
  178. int retry_cnt = 0;
  179. /* Turn interrupts off */
  180. syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
  181. syscfg &= ~ONENAND_SYS_CFG1_IOBE;
  182. write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
  183. timeout = jiffies + msecs_to_jiffies(20);
  184. while (1) {
  185. if (time_before(jiffies, timeout)) {
  186. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  187. if (intr & ONENAND_INT_MASTER)
  188. break;
  189. } else {
  190. /* Timeout after 20ms */
  191. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  192. if (ctrl & ONENAND_CTRL_ONGO) {
  193. /*
  194. * The operation seems to be still going
  195. * so give it some more time.
  196. */
  197. retry_cnt += 1;
  198. if (retry_cnt < 3) {
  199. timeout = jiffies +
  200. msecs_to_jiffies(20);
  201. continue;
  202. }
  203. }
  204. break;
  205. }
  206. }
  207. }
  208. intr = read_reg(c, ONENAND_REG_INTERRUPT);
  209. ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
  210. if (intr & ONENAND_INT_READ) {
  211. int ecc = read_reg(c, ONENAND_REG_ECC_STATUS);
  212. if (ecc) {
  213. unsigned int addr1, addr8;
  214. addr1 = read_reg(c, ONENAND_REG_START_ADDRESS1);
  215. addr8 = read_reg(c, ONENAND_REG_START_ADDRESS8);
  216. if (ecc & ONENAND_ECC_2BIT_ALL) {
  217. printk(KERN_ERR "onenand_wait: ECC error = "
  218. "0x%04x, addr1 %#x, addr8 %#x\n",
  219. ecc, addr1, addr8);
  220. mtd->ecc_stats.failed++;
  221. return -EBADMSG;
  222. } else if (ecc & ONENAND_ECC_1BIT_ALL) {
  223. printk(KERN_NOTICE "onenand_wait: correctable "
  224. "ECC error = 0x%04x, addr1 %#x, "
  225. "addr8 %#x\n", ecc, addr1, addr8);
  226. mtd->ecc_stats.corrected++;
  227. }
  228. }
  229. } else if (state == FL_READING) {
  230. wait_err("timeout", state, ctrl, intr);
  231. return -EIO;
  232. }
  233. if (ctrl & ONENAND_CTRL_ERROR) {
  234. wait_err("controller error", state, ctrl, intr);
  235. if (ctrl & ONENAND_CTRL_LOCK)
  236. printk(KERN_ERR "onenand_wait: "
  237. "Device is write protected!!!\n");
  238. return -EIO;
  239. }
  240. ctrl_mask = 0xFE9F;
  241. if (this->ongoing)
  242. ctrl_mask &= ~0x8000;
  243. if (ctrl & ctrl_mask)
  244. wait_warn("unexpected controller status", state, ctrl, intr);
  245. return 0;
  246. }
  247. static inline int omap2_onenand_bufferram_offset(struct mtd_info *mtd, int area)
  248. {
  249. struct onenand_chip *this = mtd->priv;
  250. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  251. if (area == ONENAND_DATARAM)
  252. return this->writesize;
  253. if (area == ONENAND_SPARERAM)
  254. return mtd->oobsize;
  255. }
  256. return 0;
  257. }
  258. #if defined(CONFIG_ARCH_OMAP3) || defined(MULTI_OMAP2)
  259. static int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
  260. unsigned char *buffer, int offset,
  261. size_t count)
  262. {
  263. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  264. struct onenand_chip *this = mtd->priv;
  265. dma_addr_t dma_src, dma_dst;
  266. int bram_offset;
  267. unsigned long timeout;
  268. void *buf = (void *)buffer;
  269. size_t xtra;
  270. volatile unsigned *done;
  271. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  272. if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
  273. goto out_copy;
  274. /* panic_write() may be in an interrupt context */
  275. if (in_interrupt() || oops_in_progress)
  276. goto out_copy;
  277. if (buf >= high_memory) {
  278. struct page *p1;
  279. if (((size_t)buf & PAGE_MASK) !=
  280. ((size_t)(buf + count - 1) & PAGE_MASK))
  281. goto out_copy;
  282. p1 = vmalloc_to_page(buf);
  283. if (!p1)
  284. goto out_copy;
  285. buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
  286. }
  287. xtra = count & 3;
  288. if (xtra) {
  289. count -= xtra;
  290. memcpy(buf + count, this->base + bram_offset + count, xtra);
  291. }
  292. dma_src = c->phys_base + bram_offset;
  293. dma_dst = dma_map_single(&c->pdev->dev, buf, count, DMA_FROM_DEVICE);
  294. if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
  295. dev_err(&c->pdev->dev,
  296. "Couldn't DMA map a %d byte buffer\n",
  297. count);
  298. goto out_copy;
  299. }
  300. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
  301. count >> 2, 1, 0, 0, 0);
  302. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  303. dma_src, 0, 0);
  304. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  305. dma_dst, 0, 0);
  306. reinit_completion(&c->dma_done);
  307. omap_start_dma(c->dma_channel);
  308. timeout = jiffies + msecs_to_jiffies(20);
  309. done = &c->dma_done.done;
  310. while (time_before(jiffies, timeout))
  311. if (*done)
  312. break;
  313. dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
  314. if (!*done) {
  315. dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
  316. goto out_copy;
  317. }
  318. return 0;
  319. out_copy:
  320. memcpy(buf, this->base + bram_offset, count);
  321. return 0;
  322. }
  323. static int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
  324. const unsigned char *buffer,
  325. int offset, size_t count)
  326. {
  327. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  328. struct onenand_chip *this = mtd->priv;
  329. dma_addr_t dma_src, dma_dst;
  330. int bram_offset;
  331. unsigned long timeout;
  332. void *buf = (void *)buffer;
  333. volatile unsigned *done;
  334. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  335. if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
  336. goto out_copy;
  337. /* panic_write() may be in an interrupt context */
  338. if (in_interrupt() || oops_in_progress)
  339. goto out_copy;
  340. if (buf >= high_memory) {
  341. struct page *p1;
  342. if (((size_t)buf & PAGE_MASK) !=
  343. ((size_t)(buf + count - 1) & PAGE_MASK))
  344. goto out_copy;
  345. p1 = vmalloc_to_page(buf);
  346. if (!p1)
  347. goto out_copy;
  348. buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
  349. }
  350. dma_src = dma_map_single(&c->pdev->dev, buf, count, DMA_TO_DEVICE);
  351. dma_dst = c->phys_base + bram_offset;
  352. if (dma_mapping_error(&c->pdev->dev, dma_src)) {
  353. dev_err(&c->pdev->dev,
  354. "Couldn't DMA map a %d byte buffer\n",
  355. count);
  356. return -1;
  357. }
  358. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
  359. count >> 2, 1, 0, 0, 0);
  360. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  361. dma_src, 0, 0);
  362. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  363. dma_dst, 0, 0);
  364. reinit_completion(&c->dma_done);
  365. omap_start_dma(c->dma_channel);
  366. timeout = jiffies + msecs_to_jiffies(20);
  367. done = &c->dma_done.done;
  368. while (time_before(jiffies, timeout))
  369. if (*done)
  370. break;
  371. dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
  372. if (!*done) {
  373. dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
  374. goto out_copy;
  375. }
  376. return 0;
  377. out_copy:
  378. memcpy(this->base + bram_offset, buf, count);
  379. return 0;
  380. }
  381. #else
  382. static int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
  383. unsigned char *buffer, int offset,
  384. size_t count)
  385. {
  386. return -ENOSYS;
  387. }
  388. static int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
  389. const unsigned char *buffer,
  390. int offset, size_t count)
  391. {
  392. return -ENOSYS;
  393. }
  394. #endif
  395. #if defined(CONFIG_ARCH_OMAP2) || defined(MULTI_OMAP2)
  396. static int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
  397. unsigned char *buffer, int offset,
  398. size_t count)
  399. {
  400. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  401. struct onenand_chip *this = mtd->priv;
  402. dma_addr_t dma_src, dma_dst;
  403. int bram_offset;
  404. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  405. /* DMA is not used. Revisit PM requirements before enabling it. */
  406. if (1 || (c->dma_channel < 0) ||
  407. ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
  408. (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
  409. memcpy(buffer, (__force void *)(this->base + bram_offset),
  410. count);
  411. return 0;
  412. }
  413. dma_src = c->phys_base + bram_offset;
  414. dma_dst = dma_map_single(&c->pdev->dev, buffer, count,
  415. DMA_FROM_DEVICE);
  416. if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
  417. dev_err(&c->pdev->dev,
  418. "Couldn't DMA map a %d byte buffer\n",
  419. count);
  420. return -1;
  421. }
  422. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
  423. count / 4, 1, 0, 0, 0);
  424. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  425. dma_src, 0, 0);
  426. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  427. dma_dst, 0, 0);
  428. reinit_completion(&c->dma_done);
  429. omap_start_dma(c->dma_channel);
  430. wait_for_completion(&c->dma_done);
  431. dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
  432. return 0;
  433. }
  434. static int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
  435. const unsigned char *buffer,
  436. int offset, size_t count)
  437. {
  438. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  439. struct onenand_chip *this = mtd->priv;
  440. dma_addr_t dma_src, dma_dst;
  441. int bram_offset;
  442. bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
  443. /* DMA is not used. Revisit PM requirements before enabling it. */
  444. if (1 || (c->dma_channel < 0) ||
  445. ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
  446. (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
  447. memcpy((__force void *)(this->base + bram_offset), buffer,
  448. count);
  449. return 0;
  450. }
  451. dma_src = dma_map_single(&c->pdev->dev, (void *) buffer, count,
  452. DMA_TO_DEVICE);
  453. dma_dst = c->phys_base + bram_offset;
  454. if (dma_mapping_error(&c->pdev->dev, dma_src)) {
  455. dev_err(&c->pdev->dev,
  456. "Couldn't DMA map a %d byte buffer\n",
  457. count);
  458. return -1;
  459. }
  460. omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S16,
  461. count / 2, 1, 0, 0, 0);
  462. omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  463. dma_src, 0, 0);
  464. omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
  465. dma_dst, 0, 0);
  466. reinit_completion(&c->dma_done);
  467. omap_start_dma(c->dma_channel);
  468. wait_for_completion(&c->dma_done);
  469. dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
  470. return 0;
  471. }
  472. #else
  473. static int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
  474. unsigned char *buffer, int offset,
  475. size_t count)
  476. {
  477. return -ENOSYS;
  478. }
  479. static int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
  480. const unsigned char *buffer,
  481. int offset, size_t count)
  482. {
  483. return -ENOSYS;
  484. }
  485. #endif
  486. static struct platform_driver omap2_onenand_driver;
  487. static void omap2_onenand_shutdown(struct platform_device *pdev)
  488. {
  489. struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
  490. /* With certain content in the buffer RAM, the OMAP boot ROM code
  491. * can recognize the flash chip incorrectly. Zero it out before
  492. * soft reset.
  493. */
  494. memset((__force void *)c->onenand.base, 0, ONENAND_BUFRAM_SIZE);
  495. }
  496. static int omap2_onenand_enable(struct mtd_info *mtd)
  497. {
  498. int ret;
  499. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  500. ret = regulator_enable(c->regulator);
  501. if (ret != 0)
  502. dev_err(&c->pdev->dev, "can't enable regulator\n");
  503. return ret;
  504. }
  505. static int omap2_onenand_disable(struct mtd_info *mtd)
  506. {
  507. int ret;
  508. struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
  509. ret = regulator_disable(c->regulator);
  510. if (ret != 0)
  511. dev_err(&c->pdev->dev, "can't disable regulator\n");
  512. return ret;
  513. }
  514. static int omap2_onenand_probe(struct platform_device *pdev)
  515. {
  516. struct omap_onenand_platform_data *pdata;
  517. struct omap2_onenand *c;
  518. struct onenand_chip *this;
  519. int r;
  520. struct resource *res;
  521. pdata = dev_get_platdata(&pdev->dev);
  522. if (pdata == NULL) {
  523. dev_err(&pdev->dev, "platform data missing\n");
  524. return -ENODEV;
  525. }
  526. c = kzalloc(sizeof(struct omap2_onenand), GFP_KERNEL);
  527. if (!c)
  528. return -ENOMEM;
  529. init_completion(&c->irq_done);
  530. init_completion(&c->dma_done);
  531. c->flags = pdata->flags;
  532. c->gpmc_cs = pdata->cs;
  533. c->gpio_irq = pdata->gpio_irq;
  534. c->dma_channel = pdata->dma_channel;
  535. if (c->dma_channel < 0) {
  536. /* if -1, don't use DMA */
  537. c->gpio_irq = 0;
  538. }
  539. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  540. if (res == NULL) {
  541. r = -EINVAL;
  542. dev_err(&pdev->dev, "error getting memory resource\n");
  543. goto err_kfree;
  544. }
  545. c->phys_base = res->start;
  546. c->mem_size = resource_size(res);
  547. if (request_mem_region(c->phys_base, c->mem_size,
  548. pdev->dev.driver->name) == NULL) {
  549. dev_err(&pdev->dev, "Cannot reserve memory region at 0x%08lx, size: 0x%x\n",
  550. c->phys_base, c->mem_size);
  551. r = -EBUSY;
  552. goto err_kfree;
  553. }
  554. c->onenand.base = ioremap(c->phys_base, c->mem_size);
  555. if (c->onenand.base == NULL) {
  556. r = -ENOMEM;
  557. goto err_release_mem_region;
  558. }
  559. if (pdata->onenand_setup != NULL) {
  560. r = pdata->onenand_setup(c->onenand.base, &c->freq);
  561. if (r < 0) {
  562. dev_err(&pdev->dev, "Onenand platform setup failed: "
  563. "%d\n", r);
  564. goto err_iounmap;
  565. }
  566. c->setup = pdata->onenand_setup;
  567. }
  568. if (c->gpio_irq) {
  569. if ((r = gpio_request(c->gpio_irq, "OneNAND irq")) < 0) {
  570. dev_err(&pdev->dev, "Failed to request GPIO%d for "
  571. "OneNAND\n", c->gpio_irq);
  572. goto err_iounmap;
  573. }
  574. gpio_direction_input(c->gpio_irq);
  575. if ((r = request_irq(gpio_to_irq(c->gpio_irq),
  576. omap2_onenand_interrupt, IRQF_TRIGGER_RISING,
  577. pdev->dev.driver->name, c)) < 0)
  578. goto err_release_gpio;
  579. }
  580. if (c->dma_channel >= 0) {
  581. r = omap_request_dma(0, pdev->dev.driver->name,
  582. omap2_onenand_dma_cb, (void *) c,
  583. &c->dma_channel);
  584. if (r == 0) {
  585. omap_set_dma_write_mode(c->dma_channel,
  586. OMAP_DMA_WRITE_NON_POSTED);
  587. omap_set_dma_src_data_pack(c->dma_channel, 1);
  588. omap_set_dma_src_burst_mode(c->dma_channel,
  589. OMAP_DMA_DATA_BURST_8);
  590. omap_set_dma_dest_data_pack(c->dma_channel, 1);
  591. omap_set_dma_dest_burst_mode(c->dma_channel,
  592. OMAP_DMA_DATA_BURST_8);
  593. } else {
  594. dev_info(&pdev->dev,
  595. "failed to allocate DMA for OneNAND, "
  596. "using PIO instead\n");
  597. c->dma_channel = -1;
  598. }
  599. }
  600. dev_info(&pdev->dev, "initializing on CS%d, phys base 0x%08lx, virtual "
  601. "base %p, freq %d MHz\n", c->gpmc_cs, c->phys_base,
  602. c->onenand.base, c->freq);
  603. c->pdev = pdev;
  604. c->mtd.priv = &c->onenand;
  605. c->mtd.dev.parent = &pdev->dev;
  606. mtd_set_of_node(&c->mtd, pdata->of_node);
  607. this = &c->onenand;
  608. if (c->dma_channel >= 0) {
  609. this->wait = omap2_onenand_wait;
  610. if (c->flags & ONENAND_IN_OMAP34XX) {
  611. this->read_bufferram = omap3_onenand_read_bufferram;
  612. this->write_bufferram = omap3_onenand_write_bufferram;
  613. } else {
  614. this->read_bufferram = omap2_onenand_read_bufferram;
  615. this->write_bufferram = omap2_onenand_write_bufferram;
  616. }
  617. }
  618. if (pdata->regulator_can_sleep) {
  619. c->regulator = regulator_get(&pdev->dev, "vonenand");
  620. if (IS_ERR(c->regulator)) {
  621. dev_err(&pdev->dev, "Failed to get regulator\n");
  622. r = PTR_ERR(c->regulator);
  623. goto err_release_dma;
  624. }
  625. c->onenand.enable = omap2_onenand_enable;
  626. c->onenand.disable = omap2_onenand_disable;
  627. }
  628. if (pdata->skip_initial_unlocking)
  629. this->options |= ONENAND_SKIP_INITIAL_UNLOCKING;
  630. if ((r = onenand_scan(&c->mtd, 1)) < 0)
  631. goto err_release_regulator;
  632. r = mtd_device_register(&c->mtd, pdata ? pdata->parts : NULL,
  633. pdata ? pdata->nr_parts : 0);
  634. if (r)
  635. goto err_release_onenand;
  636. platform_set_drvdata(pdev, c);
  637. return 0;
  638. err_release_onenand:
  639. onenand_release(&c->mtd);
  640. err_release_regulator:
  641. regulator_put(c->regulator);
  642. err_release_dma:
  643. if (c->dma_channel != -1)
  644. omap_free_dma(c->dma_channel);
  645. if (c->gpio_irq)
  646. free_irq(gpio_to_irq(c->gpio_irq), c);
  647. err_release_gpio:
  648. if (c->gpio_irq)
  649. gpio_free(c->gpio_irq);
  650. err_iounmap:
  651. iounmap(c->onenand.base);
  652. err_release_mem_region:
  653. release_mem_region(c->phys_base, c->mem_size);
  654. err_kfree:
  655. kfree(c);
  656. return r;
  657. }
  658. static int omap2_onenand_remove(struct platform_device *pdev)
  659. {
  660. struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
  661. onenand_release(&c->mtd);
  662. regulator_put(c->regulator);
  663. if (c->dma_channel != -1)
  664. omap_free_dma(c->dma_channel);
  665. omap2_onenand_shutdown(pdev);
  666. if (c->gpio_irq) {
  667. free_irq(gpio_to_irq(c->gpio_irq), c);
  668. gpio_free(c->gpio_irq);
  669. }
  670. iounmap(c->onenand.base);
  671. release_mem_region(c->phys_base, c->mem_size);
  672. kfree(c);
  673. return 0;
  674. }
  675. static struct platform_driver omap2_onenand_driver = {
  676. .probe = omap2_onenand_probe,
  677. .remove = omap2_onenand_remove,
  678. .shutdown = omap2_onenand_shutdown,
  679. .driver = {
  680. .name = DRIVER_NAME,
  681. },
  682. };
  683. module_platform_driver(omap2_onenand_driver);
  684. MODULE_ALIAS("platform:" DRIVER_NAME);
  685. MODULE_LICENSE("GPL");
  686. MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
  687. MODULE_DESCRIPTION("Glue layer for OneNAND flash on OMAP2 / OMAP3");