gpmc.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  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 version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/ioport.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <asm/mach-types.h>
  27. #include <plat/gpmc.h>
  28. #include <plat/cpu.h>
  29. #include <plat/gpmc.h>
  30. #include <plat/sdrc.h>
  31. #include <plat/omap_device.h>
  32. #include "soc.h"
  33. #include "common.h"
  34. #define DEVICE_NAME "omap-gpmc"
  35. /* GPMC register offsets */
  36. #define GPMC_REVISION 0x00
  37. #define GPMC_SYSCONFIG 0x10
  38. #define GPMC_SYSSTATUS 0x14
  39. #define GPMC_IRQSTATUS 0x18
  40. #define GPMC_IRQENABLE 0x1c
  41. #define GPMC_TIMEOUT_CONTROL 0x40
  42. #define GPMC_ERR_ADDRESS 0x44
  43. #define GPMC_ERR_TYPE 0x48
  44. #define GPMC_CONFIG 0x50
  45. #define GPMC_STATUS 0x54
  46. #define GPMC_PREFETCH_CONFIG1 0x1e0
  47. #define GPMC_PREFETCH_CONFIG2 0x1e4
  48. #define GPMC_PREFETCH_CONTROL 0x1ec
  49. #define GPMC_PREFETCH_STATUS 0x1f0
  50. #define GPMC_ECC_CONFIG 0x1f4
  51. #define GPMC_ECC_CONTROL 0x1f8
  52. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  53. #define GPMC_ECC1_RESULT 0x200
  54. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  55. /* GPMC ECC control settings */
  56. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  57. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  58. #define GPMC_ECC_CTRL_ECCREG1 0x001
  59. #define GPMC_ECC_CTRL_ECCREG2 0x002
  60. #define GPMC_ECC_CTRL_ECCREG3 0x003
  61. #define GPMC_ECC_CTRL_ECCREG4 0x004
  62. #define GPMC_ECC_CTRL_ECCREG5 0x005
  63. #define GPMC_ECC_CTRL_ECCREG6 0x006
  64. #define GPMC_ECC_CTRL_ECCREG7 0x007
  65. #define GPMC_ECC_CTRL_ECCREG8 0x008
  66. #define GPMC_ECC_CTRL_ECCREG9 0x009
  67. #define GPMC_CS0_OFFSET 0x60
  68. #define GPMC_CS_SIZE 0x30
  69. #define GPMC_MEM_START 0x00000000
  70. #define GPMC_MEM_END 0x3FFFFFFF
  71. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  72. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  73. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  74. #define CS_NUM_SHIFT 24
  75. #define ENABLE_PREFETCH (0x1 << 7)
  76. #define DMA_MPU_MODE 2
  77. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  78. */
  79. #define GPMC_NR_IRQ 2
  80. struct gpmc_client_irq {
  81. unsigned irq;
  82. u32 bitmask;
  83. };
  84. /* Structure to save gpmc cs context */
  85. struct gpmc_cs_config {
  86. u32 config1;
  87. u32 config2;
  88. u32 config3;
  89. u32 config4;
  90. u32 config5;
  91. u32 config6;
  92. u32 config7;
  93. int is_valid;
  94. };
  95. /*
  96. * Structure to save/restore gpmc context
  97. * to support core off on OMAP3
  98. */
  99. struct omap3_gpmc_regs {
  100. u32 sysconfig;
  101. u32 irqenable;
  102. u32 timeout_ctrl;
  103. u32 config;
  104. u32 prefetch_config1;
  105. u32 prefetch_config2;
  106. u32 prefetch_control;
  107. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  108. };
  109. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  110. static struct irq_chip gpmc_irq_chip;
  111. static unsigned gpmc_irq_start;
  112. static struct resource gpmc_mem_root;
  113. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  114. static DEFINE_SPINLOCK(gpmc_mem_lock);
  115. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  116. static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
  117. static void __iomem *gpmc_base;
  118. static struct clk *gpmc_l3_clk;
  119. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  120. static void gpmc_write_reg(int idx, u32 val)
  121. {
  122. __raw_writel(val, gpmc_base + idx);
  123. }
  124. static u32 gpmc_read_reg(int idx)
  125. {
  126. return __raw_readl(gpmc_base + idx);
  127. }
  128. static void gpmc_cs_write_byte(int cs, int idx, u8 val)
  129. {
  130. void __iomem *reg_addr;
  131. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  132. __raw_writeb(val, reg_addr);
  133. }
  134. static u8 gpmc_cs_read_byte(int cs, int idx)
  135. {
  136. void __iomem *reg_addr;
  137. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  138. return __raw_readb(reg_addr);
  139. }
  140. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  141. {
  142. void __iomem *reg_addr;
  143. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  144. __raw_writel(val, reg_addr);
  145. }
  146. u32 gpmc_cs_read_reg(int cs, int idx)
  147. {
  148. void __iomem *reg_addr;
  149. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  150. return __raw_readl(reg_addr);
  151. }
  152. /* TODO: Add support for gpmc_fck to clock framework and use it */
  153. unsigned long gpmc_get_fclk_period(void)
  154. {
  155. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  156. if (rate == 0) {
  157. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  158. return 0;
  159. }
  160. rate /= 1000;
  161. rate = 1000000000 / rate; /* In picoseconds */
  162. return rate;
  163. }
  164. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  165. {
  166. unsigned long tick_ps;
  167. /* Calculate in picosecs to yield more exact results */
  168. tick_ps = gpmc_get_fclk_period();
  169. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  170. }
  171. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  172. {
  173. unsigned long tick_ps;
  174. /* Calculate in picosecs to yield more exact results */
  175. tick_ps = gpmc_get_fclk_period();
  176. return (time_ps + tick_ps - 1) / tick_ps;
  177. }
  178. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  179. {
  180. return ticks * gpmc_get_fclk_period() / 1000;
  181. }
  182. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  183. {
  184. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  185. return ticks * gpmc_get_fclk_period() / 1000;
  186. }
  187. #ifdef DEBUG
  188. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  189. int time, const char *name)
  190. #else
  191. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  192. int time)
  193. #endif
  194. {
  195. u32 l;
  196. int ticks, mask, nr_bits;
  197. if (time == 0)
  198. ticks = 0;
  199. else
  200. ticks = gpmc_ns_to_ticks(time);
  201. nr_bits = end_bit - st_bit + 1;
  202. if (ticks >= 1 << nr_bits) {
  203. #ifdef DEBUG
  204. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  205. cs, name, time, ticks, 1 << nr_bits);
  206. #endif
  207. return -1;
  208. }
  209. mask = (1 << nr_bits) - 1;
  210. l = gpmc_cs_read_reg(cs, reg);
  211. #ifdef DEBUG
  212. printk(KERN_INFO
  213. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  214. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  215. (l >> st_bit) & mask, time);
  216. #endif
  217. l &= ~(mask << st_bit);
  218. l |= ticks << st_bit;
  219. gpmc_cs_write_reg(cs, reg, l);
  220. return 0;
  221. }
  222. #ifdef DEBUG
  223. #define GPMC_SET_ONE(reg, st, end, field) \
  224. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  225. t->field, #field) < 0) \
  226. return -1
  227. #else
  228. #define GPMC_SET_ONE(reg, st, end, field) \
  229. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  230. return -1
  231. #endif
  232. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  233. {
  234. int div;
  235. u32 l;
  236. l = sync_clk + (gpmc_get_fclk_period() - 1);
  237. div = l / gpmc_get_fclk_period();
  238. if (div > 4)
  239. return -1;
  240. if (div <= 0)
  241. div = 1;
  242. return div;
  243. }
  244. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  245. {
  246. int div;
  247. u32 l;
  248. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  249. if (div < 0)
  250. return div;
  251. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  252. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  253. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  254. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  255. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  256. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  257. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  258. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  259. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  260. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  261. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  262. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  263. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  264. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  265. if (cpu_is_omap34xx()) {
  266. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  267. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  268. }
  269. /* caller is expected to have initialized CONFIG1 to cover
  270. * at least sync vs async
  271. */
  272. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  273. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  274. #ifdef DEBUG
  275. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  276. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  277. #endif
  278. l &= ~0x03;
  279. l |= (div - 1);
  280. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  281. }
  282. return 0;
  283. }
  284. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  285. {
  286. u32 l;
  287. u32 mask;
  288. mask = (1 << GPMC_SECTION_SHIFT) - size;
  289. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  290. l &= ~0x3f;
  291. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  292. l &= ~(0x0f << 8);
  293. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  294. l |= GPMC_CONFIG7_CSVALID;
  295. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  296. }
  297. static void gpmc_cs_disable_mem(int cs)
  298. {
  299. u32 l;
  300. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  301. l &= ~GPMC_CONFIG7_CSVALID;
  302. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  303. }
  304. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  305. {
  306. u32 l;
  307. u32 mask;
  308. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  309. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  310. mask = (l >> 8) & 0x0f;
  311. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  312. }
  313. static int gpmc_cs_mem_enabled(int cs)
  314. {
  315. u32 l;
  316. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  317. return l & GPMC_CONFIG7_CSVALID;
  318. }
  319. int gpmc_cs_set_reserved(int cs, int reserved)
  320. {
  321. if (cs > GPMC_CS_NUM)
  322. return -ENODEV;
  323. gpmc_cs_map &= ~(1 << cs);
  324. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  325. return 0;
  326. }
  327. int gpmc_cs_reserved(int cs)
  328. {
  329. if (cs > GPMC_CS_NUM)
  330. return -ENODEV;
  331. return gpmc_cs_map & (1 << cs);
  332. }
  333. static unsigned long gpmc_mem_align(unsigned long size)
  334. {
  335. int order;
  336. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  337. order = GPMC_CHUNK_SHIFT - 1;
  338. do {
  339. size >>= 1;
  340. order++;
  341. } while (size);
  342. size = 1 << order;
  343. return size;
  344. }
  345. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  346. {
  347. struct resource *res = &gpmc_cs_mem[cs];
  348. int r;
  349. size = gpmc_mem_align(size);
  350. spin_lock(&gpmc_mem_lock);
  351. res->start = base;
  352. res->end = base + size - 1;
  353. r = request_resource(&gpmc_mem_root, res);
  354. spin_unlock(&gpmc_mem_lock);
  355. return r;
  356. }
  357. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  358. {
  359. struct resource *res = &gpmc_cs_mem[cs];
  360. int r = -1;
  361. if (cs > GPMC_CS_NUM)
  362. return -ENODEV;
  363. size = gpmc_mem_align(size);
  364. if (size > (1 << GPMC_SECTION_SHIFT))
  365. return -ENOMEM;
  366. spin_lock(&gpmc_mem_lock);
  367. if (gpmc_cs_reserved(cs)) {
  368. r = -EBUSY;
  369. goto out;
  370. }
  371. if (gpmc_cs_mem_enabled(cs))
  372. r = adjust_resource(res, res->start & ~(size - 1), size);
  373. if (r < 0)
  374. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  375. size, NULL, NULL);
  376. if (r < 0)
  377. goto out;
  378. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  379. *base = res->start;
  380. gpmc_cs_set_reserved(cs, 1);
  381. out:
  382. spin_unlock(&gpmc_mem_lock);
  383. return r;
  384. }
  385. EXPORT_SYMBOL(gpmc_cs_request);
  386. void gpmc_cs_free(int cs)
  387. {
  388. spin_lock(&gpmc_mem_lock);
  389. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  390. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  391. BUG();
  392. spin_unlock(&gpmc_mem_lock);
  393. return;
  394. }
  395. gpmc_cs_disable_mem(cs);
  396. release_resource(&gpmc_cs_mem[cs]);
  397. gpmc_cs_set_reserved(cs, 0);
  398. spin_unlock(&gpmc_mem_lock);
  399. }
  400. EXPORT_SYMBOL(gpmc_cs_free);
  401. /**
  402. * gpmc_read_status - read access request to get the different gpmc status
  403. * @cmd: command type
  404. * @return status
  405. */
  406. int gpmc_read_status(int cmd)
  407. {
  408. int status = -EINVAL;
  409. u32 regval = 0;
  410. switch (cmd) {
  411. case GPMC_GET_IRQ_STATUS:
  412. status = gpmc_read_reg(GPMC_IRQSTATUS);
  413. break;
  414. case GPMC_PREFETCH_FIFO_CNT:
  415. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  416. status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
  417. break;
  418. case GPMC_PREFETCH_COUNT:
  419. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  420. status = GPMC_PREFETCH_STATUS_COUNT(regval);
  421. break;
  422. case GPMC_STATUS_BUFFER:
  423. regval = gpmc_read_reg(GPMC_STATUS);
  424. /* 1 : buffer is available to write */
  425. status = regval & GPMC_STATUS_BUFF_EMPTY;
  426. break;
  427. default:
  428. printk(KERN_ERR "gpmc_read_status: Not supported\n");
  429. }
  430. return status;
  431. }
  432. EXPORT_SYMBOL(gpmc_read_status);
  433. /**
  434. * gpmc_cs_configure - write request to configure gpmc
  435. * @cs: chip select number
  436. * @cmd: command type
  437. * @wval: value to write
  438. * @return status of the operation
  439. */
  440. int gpmc_cs_configure(int cs, int cmd, int wval)
  441. {
  442. int err = 0;
  443. u32 regval = 0;
  444. switch (cmd) {
  445. case GPMC_ENABLE_IRQ:
  446. gpmc_write_reg(GPMC_IRQENABLE, wval);
  447. break;
  448. case GPMC_SET_IRQ_STATUS:
  449. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  450. break;
  451. case GPMC_CONFIG_WP:
  452. regval = gpmc_read_reg(GPMC_CONFIG);
  453. if (wval)
  454. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  455. else
  456. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  457. gpmc_write_reg(GPMC_CONFIG, regval);
  458. break;
  459. case GPMC_CONFIG_RDY_BSY:
  460. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  461. if (wval)
  462. regval |= WR_RD_PIN_MONITORING;
  463. else
  464. regval &= ~WR_RD_PIN_MONITORING;
  465. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  466. break;
  467. case GPMC_CONFIG_DEV_SIZE:
  468. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  469. /* clear 2 target bits */
  470. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  471. /* set the proper value */
  472. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  473. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  474. break;
  475. case GPMC_CONFIG_DEV_TYPE:
  476. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  477. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  478. if (wval == GPMC_DEVICETYPE_NOR)
  479. regval |= GPMC_CONFIG1_MUXADDDATA;
  480. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  481. break;
  482. default:
  483. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  484. err = -EINVAL;
  485. }
  486. return err;
  487. }
  488. EXPORT_SYMBOL(gpmc_cs_configure);
  489. /**
  490. * gpmc_nand_read - nand specific read access request
  491. * @cs: chip select number
  492. * @cmd: command type
  493. */
  494. int gpmc_nand_read(int cs, int cmd)
  495. {
  496. int rval = -EINVAL;
  497. switch (cmd) {
  498. case GPMC_NAND_DATA:
  499. rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
  500. break;
  501. default:
  502. printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
  503. }
  504. return rval;
  505. }
  506. EXPORT_SYMBOL(gpmc_nand_read);
  507. /**
  508. * gpmc_nand_write - nand specific write request
  509. * @cs: chip select number
  510. * @cmd: command type
  511. * @wval: value to write
  512. */
  513. int gpmc_nand_write(int cs, int cmd, int wval)
  514. {
  515. int err = 0;
  516. switch (cmd) {
  517. case GPMC_NAND_COMMAND:
  518. gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
  519. break;
  520. case GPMC_NAND_ADDRESS:
  521. gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
  522. break;
  523. case GPMC_NAND_DATA:
  524. gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
  525. default:
  526. printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
  527. err = -EINVAL;
  528. }
  529. return err;
  530. }
  531. EXPORT_SYMBOL(gpmc_nand_write);
  532. /**
  533. * gpmc_prefetch_enable - configures and starts prefetch transfer
  534. * @cs: cs (chip select) number
  535. * @fifo_th: fifo threshold to be used for read/ write
  536. * @dma_mode: dma mode enable (1) or disable (0)
  537. * @u32_count: number of bytes to be transferred
  538. * @is_write: prefetch read(0) or write post(1) mode
  539. */
  540. int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
  541. unsigned int u32_count, int is_write)
  542. {
  543. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
  544. pr_err("gpmc: fifo threshold is not supported\n");
  545. return -1;
  546. } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
  547. /* Set the amount of bytes to be prefetched */
  548. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
  549. /* Set dma/mpu mode, the prefetch read / post write and
  550. * enable the engine. Set which cs is has requested for.
  551. */
  552. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
  553. PREFETCH_FIFOTHRESHOLD(fifo_th) |
  554. ENABLE_PREFETCH |
  555. (dma_mode << DMA_MPU_MODE) |
  556. (0x1 & is_write)));
  557. /* Start the prefetch engine */
  558. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
  559. } else {
  560. return -EBUSY;
  561. }
  562. return 0;
  563. }
  564. EXPORT_SYMBOL(gpmc_prefetch_enable);
  565. /**
  566. * gpmc_prefetch_reset - disables and stops the prefetch engine
  567. */
  568. int gpmc_prefetch_reset(int cs)
  569. {
  570. u32 config1;
  571. /* check if the same module/cs is trying to reset */
  572. config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  573. if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
  574. return -EINVAL;
  575. /* Stop the PFPW engine */
  576. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
  577. /* Reset/disable the PFPW engine */
  578. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL(gpmc_prefetch_reset);
  582. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  583. {
  584. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  585. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  586. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  587. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  588. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  589. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  590. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  591. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  592. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  593. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  594. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  595. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  596. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  597. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  598. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  599. reg->gpmc_bch_result0 = gpmc_base + GPMC_ECC_BCH_RESULT_0;
  600. }
  601. int gpmc_get_client_irq(unsigned irq_config)
  602. {
  603. int i;
  604. if (hweight32(irq_config) > 1)
  605. return 0;
  606. for (i = 0; i < GPMC_NR_IRQ; i++)
  607. if (gpmc_client_irq[i].bitmask & irq_config)
  608. return gpmc_client_irq[i].irq;
  609. return 0;
  610. }
  611. static int gpmc_irq_endis(unsigned irq, bool endis)
  612. {
  613. int i;
  614. u32 regval;
  615. for (i = 0; i < GPMC_NR_IRQ; i++)
  616. if (irq == gpmc_client_irq[i].irq) {
  617. regval = gpmc_read_reg(GPMC_IRQENABLE);
  618. if (endis)
  619. regval |= gpmc_client_irq[i].bitmask;
  620. else
  621. regval &= ~gpmc_client_irq[i].bitmask;
  622. gpmc_write_reg(GPMC_IRQENABLE, regval);
  623. break;
  624. }
  625. return 0;
  626. }
  627. static void gpmc_irq_disable(struct irq_data *p)
  628. {
  629. gpmc_irq_endis(p->irq, false);
  630. }
  631. static void gpmc_irq_enable(struct irq_data *p)
  632. {
  633. gpmc_irq_endis(p->irq, true);
  634. }
  635. static void gpmc_irq_noop(struct irq_data *data) { }
  636. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  637. static int gpmc_setup_irq(int gpmc_irq)
  638. {
  639. int i;
  640. u32 regval;
  641. if (!gpmc_irq)
  642. return -EINVAL;
  643. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  644. if (IS_ERR_VALUE(gpmc_irq_start)) {
  645. pr_err("irq_alloc_descs failed\n");
  646. return gpmc_irq_start;
  647. }
  648. gpmc_irq_chip.name = "gpmc";
  649. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  650. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  651. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  652. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  653. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  654. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  655. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  656. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  657. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  658. for (i = 0; i < GPMC_NR_IRQ; i++) {
  659. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  660. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  661. &gpmc_irq_chip, handle_simple_irq);
  662. set_irq_flags(gpmc_client_irq[i].irq,
  663. IRQF_VALID | IRQF_NOAUTOEN);
  664. }
  665. /* Disable interrupts */
  666. gpmc_write_reg(GPMC_IRQENABLE, 0);
  667. /* clear interrupts */
  668. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  669. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  670. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  671. }
  672. static void __init gpmc_mem_init(void)
  673. {
  674. int cs;
  675. unsigned long boot_rom_space = 0;
  676. /* never allocate the first page, to facilitate bug detection;
  677. * even if we didn't boot from ROM.
  678. */
  679. boot_rom_space = BOOT_ROM_SPACE;
  680. /* In apollon the CS0 is mapped as 0x0000 0000 */
  681. if (machine_is_omap_apollon())
  682. boot_rom_space = 0;
  683. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  684. gpmc_mem_root.end = GPMC_MEM_END;
  685. /* Reserve all regions that has been set up by bootloader */
  686. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  687. u32 base, size;
  688. if (!gpmc_cs_mem_enabled(cs))
  689. continue;
  690. gpmc_cs_get_memconf(cs, &base, &size);
  691. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  692. BUG();
  693. }
  694. }
  695. static int __init gpmc_init(void)
  696. {
  697. u32 l;
  698. int ret = -EINVAL;
  699. int gpmc_irq;
  700. char *ck = NULL;
  701. if (cpu_is_omap24xx()) {
  702. ck = "core_l3_ck";
  703. if (cpu_is_omap2420())
  704. l = OMAP2420_GPMC_BASE;
  705. else
  706. l = OMAP34XX_GPMC_BASE;
  707. gpmc_irq = 20 + OMAP_INTC_START;
  708. } else if (cpu_is_omap34xx()) {
  709. ck = "gpmc_fck";
  710. l = OMAP34XX_GPMC_BASE;
  711. gpmc_irq = 20 + OMAP_INTC_START;
  712. } else if (cpu_is_omap44xx() || soc_is_omap54xx()) {
  713. /* Base address and irq number are same for OMAP4/5 */
  714. ck = "gpmc_ck";
  715. l = OMAP44XX_GPMC_BASE;
  716. gpmc_irq = 20 + OMAP44XX_IRQ_GIC_START;
  717. }
  718. if (WARN_ON(!ck))
  719. return ret;
  720. gpmc_l3_clk = clk_get(NULL, ck);
  721. if (IS_ERR(gpmc_l3_clk)) {
  722. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  723. BUG();
  724. }
  725. gpmc_base = ioremap(l, SZ_4K);
  726. if (!gpmc_base) {
  727. clk_put(gpmc_l3_clk);
  728. printk(KERN_ERR "Could not get GPMC register memory\n");
  729. BUG();
  730. }
  731. clk_prepare_enable(gpmc_l3_clk);
  732. l = gpmc_read_reg(GPMC_REVISION);
  733. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  734. /* Set smart idle mode and automatic L3 clock gating */
  735. l = gpmc_read_reg(GPMC_SYSCONFIG);
  736. l &= 0x03 << 3;
  737. l |= (0x02 << 3) | (1 << 0);
  738. gpmc_write_reg(GPMC_SYSCONFIG, l);
  739. gpmc_mem_init();
  740. ret = gpmc_setup_irq(gpmc_irq);
  741. if (ret)
  742. pr_err("gpmc: irq-%d could not claim: err %d\n",
  743. gpmc_irq, ret);
  744. return ret;
  745. }
  746. postcore_initcall(gpmc_init);
  747. static int __init omap_gpmc_init(void)
  748. {
  749. struct omap_hwmod *oh;
  750. struct platform_device *pdev;
  751. char *oh_name = "gpmc";
  752. oh = omap_hwmod_lookup(oh_name);
  753. if (!oh) {
  754. pr_err("Could not look up %s\n", oh_name);
  755. return -ENODEV;
  756. }
  757. pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0, NULL, 0, 0);
  758. WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
  759. return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
  760. }
  761. postcore_initcall(omap_gpmc_init);
  762. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  763. {
  764. int i;
  765. u32 regval;
  766. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  767. if (!regval)
  768. return IRQ_NONE;
  769. for (i = 0; i < GPMC_NR_IRQ; i++)
  770. if (regval & gpmc_client_irq[i].bitmask)
  771. generic_handle_irq(gpmc_client_irq[i].irq);
  772. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  773. return IRQ_HANDLED;
  774. }
  775. #ifdef CONFIG_ARCH_OMAP3
  776. static struct omap3_gpmc_regs gpmc_context;
  777. void omap3_gpmc_save_context(void)
  778. {
  779. int i;
  780. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  781. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  782. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  783. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  784. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  785. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  786. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  787. for (i = 0; i < GPMC_CS_NUM; i++) {
  788. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  789. if (gpmc_context.cs_context[i].is_valid) {
  790. gpmc_context.cs_context[i].config1 =
  791. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  792. gpmc_context.cs_context[i].config2 =
  793. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  794. gpmc_context.cs_context[i].config3 =
  795. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  796. gpmc_context.cs_context[i].config4 =
  797. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  798. gpmc_context.cs_context[i].config5 =
  799. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  800. gpmc_context.cs_context[i].config6 =
  801. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  802. gpmc_context.cs_context[i].config7 =
  803. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  804. }
  805. }
  806. }
  807. void omap3_gpmc_restore_context(void)
  808. {
  809. int i;
  810. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  811. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  812. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  813. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  814. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  815. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  816. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  817. for (i = 0; i < GPMC_CS_NUM; i++) {
  818. if (gpmc_context.cs_context[i].is_valid) {
  819. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  820. gpmc_context.cs_context[i].config1);
  821. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  822. gpmc_context.cs_context[i].config2);
  823. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  824. gpmc_context.cs_context[i].config3);
  825. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  826. gpmc_context.cs_context[i].config4);
  827. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  828. gpmc_context.cs_context[i].config5);
  829. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  830. gpmc_context.cs_context[i].config6);
  831. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  832. gpmc_context.cs_context[i].config7);
  833. }
  834. }
  835. }
  836. #endif /* CONFIG_ARCH_OMAP3 */
  837. /**
  838. * gpmc_enable_hwecc - enable hardware ecc functionality
  839. * @cs: chip select number
  840. * @mode: read/write mode
  841. * @dev_width: device bus width(1 for x16, 0 for x8)
  842. * @ecc_size: bytes for which ECC will be generated
  843. */
  844. int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
  845. {
  846. unsigned int val;
  847. /* check if ecc module is in used */
  848. if (gpmc_ecc_used != -EINVAL)
  849. return -EINVAL;
  850. gpmc_ecc_used = cs;
  851. /* clear ecc and enable bits */
  852. gpmc_write_reg(GPMC_ECC_CONTROL,
  853. GPMC_ECC_CTRL_ECCCLEAR |
  854. GPMC_ECC_CTRL_ECCREG1);
  855. /* program ecc and result sizes */
  856. val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
  857. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
  858. switch (mode) {
  859. case GPMC_ECC_READ:
  860. case GPMC_ECC_WRITE:
  861. gpmc_write_reg(GPMC_ECC_CONTROL,
  862. GPMC_ECC_CTRL_ECCCLEAR |
  863. GPMC_ECC_CTRL_ECCREG1);
  864. break;
  865. case GPMC_ECC_READSYN:
  866. gpmc_write_reg(GPMC_ECC_CONTROL,
  867. GPMC_ECC_CTRL_ECCCLEAR |
  868. GPMC_ECC_CTRL_ECCDISABLE);
  869. break;
  870. default:
  871. printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
  872. break;
  873. }
  874. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  875. val = (dev_width << 7) | (cs << 1) | (0x1);
  876. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  877. return 0;
  878. }
  879. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc);
  880. /**
  881. * gpmc_calculate_ecc - generate non-inverted ecc bytes
  882. * @cs: chip select number
  883. * @dat: data pointer over which ecc is computed
  884. * @ecc_code: ecc code buffer
  885. *
  886. * Using non-inverted ECC is considered ugly since writing a blank
  887. * page (padding) will clear the ECC bytes. This is not a problem as long
  888. * no one is trying to write data on the seemingly unused page. Reading
  889. * an erased page will produce an ECC mismatch between generated and read
  890. * ECC bytes that has to be dealt with separately.
  891. */
  892. int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
  893. {
  894. unsigned int val = 0x0;
  895. if (gpmc_ecc_used != cs)
  896. return -EINVAL;
  897. /* read ecc result */
  898. val = gpmc_read_reg(GPMC_ECC1_RESULT);
  899. *ecc_code++ = val; /* P128e, ..., P1e */
  900. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  901. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  902. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  903. gpmc_ecc_used = -EINVAL;
  904. return 0;
  905. }
  906. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc);
  907. #ifdef CONFIG_ARCH_OMAP3
  908. /**
  909. * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality
  910. * @cs: chip select number
  911. * @nsectors: how many 512-byte sectors to process
  912. * @nerrors: how many errors to correct per sector (4 or 8)
  913. *
  914. * This function must be executed before any call to gpmc_enable_hwecc_bch.
  915. */
  916. int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors)
  917. {
  918. /* check if ecc module is in use */
  919. if (gpmc_ecc_used != -EINVAL)
  920. return -EINVAL;
  921. /* support only OMAP3 class */
  922. if (!cpu_is_omap34xx()) {
  923. printk(KERN_ERR "BCH ecc is not supported on this CPU\n");
  924. return -EINVAL;
  925. }
  926. /*
  927. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
  928. * Other chips may be added if confirmed to work.
  929. */
  930. if ((nerrors == 4) &&
  931. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
  932. printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
  933. return -EINVAL;
  934. }
  935. /* sanity check */
  936. if (nsectors > 8) {
  937. printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n",
  938. nsectors);
  939. return -EINVAL;
  940. }
  941. return 0;
  942. }
  943. EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch);
  944. /**
  945. * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality
  946. * @cs: chip select number
  947. * @mode: read/write mode
  948. * @dev_width: device bus width(1 for x16, 0 for x8)
  949. * @nsectors: how many 512-byte sectors to process
  950. * @nerrors: how many errors to correct per sector (4 or 8)
  951. */
  952. int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors,
  953. int nerrors)
  954. {
  955. unsigned int val;
  956. /* check if ecc module is in use */
  957. if (gpmc_ecc_used != -EINVAL)
  958. return -EINVAL;
  959. gpmc_ecc_used = cs;
  960. /* clear ecc and enable bits */
  961. gpmc_write_reg(GPMC_ECC_CONTROL, 0x1);
  962. /*
  963. * When using BCH, sector size is hardcoded to 512 bytes.
  964. * Here we are using wrapping mode 6 both for reading and writing, with:
  965. * size0 = 0 (no additional protected byte in spare area)
  966. * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  967. */
  968. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12));
  969. /* BCH configuration */
  970. val = ((1 << 16) | /* enable BCH */
  971. (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
  972. (0x06 << 8) | /* wrap mode = 6 */
  973. (dev_width << 7) | /* bus width */
  974. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  975. (cs << 1) | /* ECC CS */
  976. (0x1)); /* enable ECC */
  977. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  978. gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
  979. return 0;
  980. }
  981. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch);
  982. /**
  983. * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes
  984. * @cs: chip select number
  985. * @dat: The pointer to data on which ecc is computed
  986. * @ecc: The ecc output buffer
  987. */
  988. int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc)
  989. {
  990. int i;
  991. unsigned long nsectors, reg, val1, val2;
  992. if (gpmc_ecc_used != cs)
  993. return -EINVAL;
  994. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  995. for (i = 0; i < nsectors; i++) {
  996. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  997. /* Read hw-computed remainder */
  998. val1 = gpmc_read_reg(reg + 0);
  999. val2 = gpmc_read_reg(reg + 4);
  1000. /*
  1001. * Add constant polynomial to remainder, in order to get an ecc
  1002. * sequence of 0xFFs for a buffer filled with 0xFFs; and
  1003. * left-justify the resulting polynomial.
  1004. */
  1005. *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
  1006. *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
  1007. *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
  1008. *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
  1009. *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
  1010. *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
  1011. *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
  1012. }
  1013. gpmc_ecc_used = -EINVAL;
  1014. return 0;
  1015. }
  1016. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4);
  1017. /**
  1018. * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes
  1019. * @cs: chip select number
  1020. * @dat: The pointer to data on which ecc is computed
  1021. * @ecc: The ecc output buffer
  1022. */
  1023. int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc)
  1024. {
  1025. int i;
  1026. unsigned long nsectors, reg, val1, val2, val3, val4;
  1027. if (gpmc_ecc_used != cs)
  1028. return -EINVAL;
  1029. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  1030. for (i = 0; i < nsectors; i++) {
  1031. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  1032. /* Read hw-computed remainder */
  1033. val1 = gpmc_read_reg(reg + 0);
  1034. val2 = gpmc_read_reg(reg + 4);
  1035. val3 = gpmc_read_reg(reg + 8);
  1036. val4 = gpmc_read_reg(reg + 12);
  1037. /*
  1038. * Add constant polynomial to remainder, in order to get an ecc
  1039. * sequence of 0xFFs for a buffer filled with 0xFFs.
  1040. */
  1041. *ecc++ = 0xef ^ (val4 & 0xFF);
  1042. *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
  1043. *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
  1044. *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
  1045. *ecc++ = 0xed ^ (val3 & 0xFF);
  1046. *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
  1047. *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
  1048. *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
  1049. *ecc++ = 0x97 ^ (val2 & 0xFF);
  1050. *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
  1051. *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
  1052. *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
  1053. *ecc++ = 0xb5 ^ (val1 & 0xFF);
  1054. }
  1055. gpmc_ecc_used = -EINVAL;
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8);
  1059. #endif /* CONFIG_ARCH_OMAP3 */