2 * Copyright (C) 2014-2015, NVIDIA Corporation. All rights reserved.
5 * Bharath H S <bhs@nvidia.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * drivers/mtd/maps/tegra_nor_gmi.c
23 * MTD mapping driver for the internal SNOR controller in Tegra SoCs
27 #include <linux/platform_device.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/map.h>
33 #include <linux/dma-mapping.h>
35 #include <linux/clk.h>
36 #include <linux/platform_data/tegra_nor.h>
37 #include <linux/mtd/concat.h>
38 #include <linux/gpio.h>
39 #include <linux/tegra_snor.h>
40 #include <linux/of_gpio.h>
41 #include "../../../arch/arm/mach-tegra/iomap.h"
42 #include <linux/delay.h>
45 #ifdef CONFIG_TEGRA_GMI_ACCESS_CONTROL
46 #include <linux/tegra_gmi_access.h>
49 struct map_info_list {
52 unsigned long long totalflashsize;
55 struct tegra_nor_info {
56 struct tegra_nor_platform_data *plat; /* Platform data */
57 struct device *dev; /* Platform device */
58 struct clk *clk; /* Clock */
59 struct mtd_partition *parts; /* MtdParts passed by
63 struct mtd_info **mtd;
64 struct mtd_info *concat_mtd;
65 struct gpio_addr_info *adinfo;
66 struct completion dma_complete; /* comletion for DMA */
67 void __iomem *base; /* SNOR register address */
68 void *dma_virt_buffer;
69 dma_addr_t dma_phys_buffer;
71 u32 timing0_default, timing1_default;
72 u32 timing0_read, timing1_read;
74 int (*request_gmi_access)(u32 gmiLockHandle);
75 void (*release_gmi_access)(void);
78 static void tegra_snor_reset_controller(struct tegra_nor_info *info);
80 /* Master structure of all tegra NOR private data*/
81 struct tegra_nor_private {
82 struct tegra_nor_info *info;
86 #define DRV_NAME "tegra-nor"
88 #define gmi_lock(info) \
90 if ((info)->request_gmi_access) \
91 (info)->request_gmi_access((info)->gmiLockHandle); \
94 #define gmi_unlock(info) \
96 if ((info)->release_gmi_access) \
97 (info)->release_gmi_access(); \
101 #if defined(CONFIG_TEGRA_EFS) || defined(CONFIG_TEGRA_PFLASH)
102 static struct map_info_list nor_gmi_map_list;
105 static inline unsigned long tegra_snor_readl(struct tegra_nor_info *tnor,
108 return readl(tnor->base + reg);
111 static inline void tegra_snor_writel(struct tegra_nor_info *tnor,
112 unsigned long val, unsigned long reg)
114 writel(val, tnor->base + reg);
118 static const char * const part_probes[] = { "cmdlinepart", NULL };
120 static int wait_for_dma_completion(struct tegra_nor_info *info)
122 unsigned long dma_timeout;
125 dma_timeout = msecs_to_jiffies(TEGRA_SNOR_DMA_TIMEOUT_MS);
126 ret = wait_for_completion_timeout(&info->dma_complete, dma_timeout);
127 return ret ? 0 : -ETIMEDOUT;
130 static void flash_bank_cs(struct map_info *map)
132 struct tegra_nor_private *priv =
133 (struct tegra_nor_private *)map->map_priv_1;
134 struct cs_info *csinfo = priv->cs;
135 struct gpio_state *state = csinfo->gpio_cs;
138 struct tegra_nor_info *c = priv->info;
140 snor_config = tegra_snor_readl(c, TEGRA_SNOR_CONFIG_REG);
141 snor_config &= SNOR_CONFIG_MASK;
142 snor_config |= TEGRA_SNOR_CONFIG_SNOR_CS(csinfo->cs);
143 tegra_snor_writel(c, snor_config, TEGRA_SNOR_CONFIG_REG);
145 c->init_config = snor_config;
147 snor_config = tegra_snor_readl(c, TEGRA_SNOR_CONFIG_REG);
148 for (i = 0; i < csinfo->num_cs_gpio; i++) {
149 if (gpio_get_value(state[i].gpio_num) != state[i].value)
150 gpio_set_value(state[i].gpio_num, state[i].value);
154 static void flash_bank_addr(struct map_info *map, unsigned int offset)
156 struct tegra_nor_private *priv =
157 (struct tegra_nor_private *)map->map_priv_1;
158 struct gpio_addr_info *adinfo = priv->info->adinfo;
159 struct gpio_addr *addr = adinfo->addr;
160 struct gpio_state state;
163 for (i = 0; i < adinfo->num_gpios; i++) {
164 state.gpio_num = addr[i].gpio_num;
165 state.value = (BIT(addr[i].line_num) & offset) ? HIGH : LOW;
166 if (gpio_get_value(state.gpio_num) != state.value)
167 gpio_set_value(state.gpio_num, state.value);
171 static void tegra_flash_dma(struct map_info *map,
172 void *to, unsigned long from, ssize_t len)
174 u32 snor_config, dma_config = 0;
175 int dma_transfer_count = 0, word32_count = 0;
176 u32 nor_address, current_transfer = 0;
177 u32 copy_to = (u32)to;
178 struct tegra_nor_private *priv =
179 (struct tegra_nor_private *)map->map_priv_1;
180 struct tegra_nor_info *c = priv->info;
181 struct tegra_nor_chip_parms *chip_parm = &c->plat->chip_parms;
182 unsigned int bytes_remaining = len;
183 unsigned int page_length = 0, pio_length = 0;
184 int dma_retry_count = 0;
185 struct pinctrl_state *s;
187 struct pinctrl_dev *pctl_dev = NULL;
189 snor_config = c->init_config;
191 tegra_snor_writel(c, c->timing0_read, TEGRA_SNOR_TIMING0_REG);
192 tegra_snor_writel(c, c->timing1_read, TEGRA_SNOR_TIMING1_REG);
194 if ((chip_parm->MuxMode == NorMuxMode_ADNonMux) &&
195 (c->plat->flash.width == 4))
196 /* keep OE low for read 0perations */
197 if (gpio_get_value(c->plat->gmi_oe_n_gpio) == HIGH)
198 gpio_set_value(c->plat->gmi_oe_n_gpio, LOW);
202 * The parameters can be setup in any order since we write to
203 * controller register only after all parameters are set.
205 /* SNOR CONFIGURATION SETUP */
207 switch (chip_parm->ReadMode) {
208 case NorReadMode_Async:
209 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(0);
212 case NorReadMode_Page:
213 switch (chip_parm->PageLength) {
214 case NorPageLength_Unsupported:
215 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(0);
218 case NorPageLength_4Word:
219 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(1);
220 snor_config |= TEGRA_SNOR_CONFIG_PAGE_SZ(1);
224 case NorPageLength_8Word:
225 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(1);
226 snor_config |= TEGRA_SNOR_CONFIG_PAGE_SZ(2);
230 case NorPageLength_16Word:
231 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(1);
232 snor_config |= TEGRA_SNOR_CONFIG_PAGE_SZ(3);
238 case NorReadMode_Burst:
239 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(2);
240 switch (chip_parm->BurstLength) {
241 case NorBurstLength_CntBurst:
242 snor_config |= TEGRA_SNOR_CONFIG_BURST_LEN(0);
244 case NorBurstLength_8Word:
245 snor_config |= TEGRA_SNOR_CONFIG_BURST_LEN(1);
248 case NorBurstLength_16Word:
249 snor_config |= TEGRA_SNOR_CONFIG_BURST_LEN(2);
252 case NorBurstLength_32Word:
253 snor_config |= TEGRA_SNOR_CONFIG_BURST_LEN(3);
259 if (chip_parm->ReadMode == NorReadMode_Page
260 && chip_parm->PageLength != 0) {
262 if (from & (page_length - 1)) {
263 pio_length = page_length -
264 (from & (page_length - 1));
266 /* Do Pio if remaining bytes are less than 32 */
267 if (pio_length + 32 >= len)
270 memcpy_fromio((char *)to,
271 ((char *)(map->virt + from)),
274 to = (void *)((u32)to + pio_length);
281 word32_count = len >> 2;
282 bytes_remaining = len & 0x00000003;
284 snor_config |= TEGRA_SNOR_CONFIG_MST_ENB;
285 /* SNOR DMA CONFIGURATION SETUP */
287 dma_config &= ~TEGRA_SNOR_DMA_CFG_DIR;
289 /* Fix this to have max burst as in commented code*/
290 /*dma_config |= TEGRA_SNOR_DMA_CFG_BRST_SZ(6);*/
291 dma_config |= TEGRA_SNOR_DMA_CFG_BRST_SZ(4);
293 tegra_snor_writel(c, snor_config, TEGRA_SNOR_CONFIG_REG);
294 tegra_snor_writel(c, dma_config, TEGRA_SNOR_DMA_CFG_REG);
296 /* Configure GMI_WAIT pinmux to RSVD1 */
297 pctl_dev = pinctrl_get_dev_from_of_compatible("nvidia,tegra124-pinmux");
298 if (pctl_dev != NULL) {
299 ret = pinctrl_set_func_for_pin(pctl_dev,
300 pinctrl_get_pin_from_pin_name(pctl_dev, c->plat->gmi_wait_pin),
303 dev_warn(c->dev, "Changing GMI_WAIT to rsvd1 mode failed\n");
306 for (nor_address = (unsigned int)(map->phys + from);
308 word32_count -= current_transfer,
309 dma_transfer_count += current_transfer,
310 nor_address += (current_transfer * 4),
311 copy_to += (current_transfer * 4)) {
314 (word32_count > TEGRA_SNOR_DMA_LIMIT_WORDS)
315 ? (TEGRA_SNOR_DMA_LIMIT_WORDS) : word32_count;
317 /* Enable interrupt before every transaction since the
318 * interrupt handler disables it */
319 dma_config |= TEGRA_SNOR_DMA_CFG_INT_ENB;
320 /* Num of AHB (32-bit) words to transferred minus 1 */
322 TEGRA_SNOR_DMA_CFG_WRD_CNT(current_transfer - 1);
323 tegra_snor_writel(c, c->dma_phys_buffer,
324 TEGRA_SNOR_AHB_ADDR_PTR_REG);
325 tegra_snor_writel(c, nor_address,
326 TEGRA_SNOR_NOR_ADDR_PTR_REG);
327 tegra_snor_writel(c, dma_config,
328 TEGRA_SNOR_DMA_CFG_REG);
330 /* GO bits for snor_config_0 and dma_config */
331 snor_config |= TEGRA_SNOR_CONFIG_GO;
332 dma_config |= TEGRA_SNOR_DMA_CFG_GO;
335 /* reset the snor controler */
336 tegra_snor_reset_controller(c);
337 /* Set NOR_ADDR, AHB_ADDR, DMA_CFG */
338 tegra_snor_writel(c, c->dma_phys_buffer,
339 TEGRA_SNOR_AHB_ADDR_PTR_REG);
340 tegra_snor_writel(c, nor_address,
341 TEGRA_SNOR_NOR_ADDR_PTR_REG);
343 /* Set GO bit for dma_config */
344 tegra_snor_writel(c, dma_config,
345 TEGRA_SNOR_DMA_CFG_REG);
347 tegra_snor_readl(c, TEGRA_SNOR_DMA_CFG_REG);
350 if (dma_retry_count > MAX_NOR_DMA_RETRIES) {
351 dev_err(c->dev, "DMA retry count exceeded.\n");
352 /* Transfer the remaining words by memcpy */
353 bytes_remaining += (word32_count << 2);
356 } while (TEGRA_SNOR_DMA_DATA_CNT(tegra_snor_readl(c, TEGRA_SNOR_STA_0)));
358 /* Reset retry count */
360 /* set GO bit for snor_config_0*/
361 tegra_snor_writel(c, snor_config,
362 TEGRA_SNOR_CONFIG_REG);
364 if (wait_for_dma_completion(c)) {
365 dev_err(c->dev, "timout waiting for DMA\n");
366 /* reset the snor controler on failure */
367 tegra_snor_reset_controller(c);
368 /* Transfer the remaining words by memcpy */
369 bytes_remaining += (word32_count << 2);
372 /* use dma map single instead of coheret buffer */
373 dma_sync_single_for_cpu(c->dev, c->dma_phys_buffer,
374 (current_transfer << 2), DMA_FROM_DEVICE);
375 memcpy((char *)(copy_to), (char *)(c->dma_virt_buffer),
376 (current_transfer << 2));
381 /* Restore GMI_WAIT pinmux back to original */
382 pctl_dev = pinctrl_get_dev_from_of_compatible("nvidia,tegra124-pinmux");
383 if (pctl_dev != NULL) {
384 ret = pinctrl_set_func_for_pin(pctl_dev,
385 pinctrl_get_pin_from_pin_name(pctl_dev, c->plat->gmi_wait_pin),
388 dev_warn(c->dev, "Chagning GMI_WAIT to gmi mode failed\n");
391 /* Put the controller back into slave mode. */
392 snor_config = tegra_snor_readl(c, TEGRA_SNOR_CONFIG_REG);
393 snor_config &= ~TEGRA_SNOR_CONFIG_MST_ENB;
394 snor_config &= ~TEGRA_SNOR_CONFIG_DEVICE_MODE(3);
395 snor_config &= ~TEGRA_SNOR_CONFIG_PAGE_SZ(7);
396 snor_config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(0);
397 tegra_snor_writel(c, snor_config, TEGRA_SNOR_CONFIG_REG);
399 memcpy_fromio(((char *)to + (dma_transfer_count << 2)),
400 ((char *)(map->virt + from) + (dma_transfer_count << 2)),
403 tegra_snor_writel(c, c->timing0_default, TEGRA_SNOR_TIMING0_REG);
404 tegra_snor_writel(c, c->timing1_default, TEGRA_SNOR_TIMING1_REG);
407 void tegra_nor_copy_from(struct map_info *map,
408 void *to, unsigned long from, ssize_t len)
411 struct tegra_nor_private *priv =
412 (struct tegra_nor_private *)map->map_priv_1;
413 struct tegra_nor_info *c = priv->info;
414 unsigned long bank_boundary;
417 /* All feature for go thru loop*/
420 /* Tegra SNOR controller window is 128MB this
421 check is required to take care of case which cross bank boundary*/
422 bank_boundary = (from & ~SNOR_WINDOW_SIZE) + SNOR_WINDOW_SIZE + 1;
424 if ((from + len) > bank_boundary) {
425 unsigned long transfer_chunk = bank_boundary - from;
427 flash_bank_addr(map, from);
428 tegra_flash_dma(map, to, (from & SNOR_WINDOW_SIZE),
429 bank_boundary - from);
431 flash_bank_addr(map, bank_boundary);
432 tegra_flash_dma(map, to + (bank_boundary - from), 0,
433 len - transfer_chunk);
435 flash_bank_addr(map, from);
436 tegra_flash_dma(map, to, (from & SNOR_WINDOW_SIZE), len);
443 map_word tegra_nor_read(struct map_info *map,
447 struct tegra_nor_private *priv =
448 (struct tegra_nor_private *)map->map_priv_1;
449 struct tegra_nor_info *c = priv->info;
450 struct tegra_nor_chip_parms *chip_parm = &c->plat->chip_parms;
454 tegra_snor_writel(c, c->init_config, TEGRA_SNOR_CONFIG_REG);
455 tegra_snor_writel(c, c->timing0_read, TEGRA_SNOR_TIMING0_REG);
456 tegra_snor_writel(c, c->timing1_read, TEGRA_SNOR_TIMING1_REG);
459 flash_bank_addr(map, ofs);
461 if ((chip_parm->MuxMode == NorMuxMode_ADNonMux) &&
462 (c->plat->flash.width == 4))
463 /* keep OE low for read operations */
464 if (gpio_get_value(c->plat->gmi_oe_n_gpio) == HIGH)
465 gpio_set_value(c->plat->gmi_oe_n_gpio, LOW);
467 ret = inline_map_read(map, ofs & SNOR_WINDOW_SIZE);
473 void tegra_nor_write(struct map_info *map,
474 map_word datum, unsigned long ofs)
477 struct tegra_nor_private *priv =
478 (struct tegra_nor_private *)map->map_priv_1;
479 struct tegra_nor_info *c = priv->info;
480 struct tegra_nor_chip_parms *chip_parm = &c->plat->chip_parms;
483 tegra_snor_writel(c, c->init_config, TEGRA_SNOR_CONFIG_REG);
484 tegra_snor_writel(c, c->timing0_read, TEGRA_SNOR_TIMING0_REG);
485 tegra_snor_writel(c, c->timing1_read, TEGRA_SNOR_TIMING1_REG);
488 flash_bank_addr(map, ofs);
490 if ((chip_parm->MuxMode == NorMuxMode_ADNonMux) &&
491 (c->plat->flash.width == 4))
492 /* keep OE high for write operations */
493 if (gpio_get_value(c->plat->gmi_oe_n_gpio) == LOW)
494 gpio_set_value(c->plat->gmi_oe_n_gpio, HIGH);
496 inline_map_write(map, datum, ofs & SNOR_WINDOW_SIZE);
501 static irqreturn_t tegra_nor_isr(int flag, void *dev_id)
503 struct tegra_nor_info *info = (struct tegra_nor_info *)dev_id;
505 u32 dma_config = tegra_snor_readl(info, TEGRA_SNOR_DMA_CFG_REG);
507 if (dma_config & TEGRA_SNOR_DMA_CFG_INT_STA) {
508 /* Disable interrupts. WAR for BUG:821560 */
509 dma_config &= ~TEGRA_SNOR_DMA_CFG_INT_ENB;
510 tegra_snor_writel(info, dma_config, TEGRA_SNOR_DMA_CFG_REG);
511 complete(&info->dma_complete);
513 pr_err("%s: Spurious interrupt\n", __func__);
518 static void tegra_snor_reset_controller(struct tegra_nor_info *info)
520 u32 snor_config, dma_config, timing0, timing1 = 0;
522 snor_config = tegra_snor_readl(info, TEGRA_SNOR_CONFIG_REG);
523 dma_config = tegra_snor_readl(info, TEGRA_SNOR_DMA_CFG_REG);
524 timing0 = tegra_snor_readl(info, TEGRA_SNOR_TIMING0_REG);
525 timing1 = tegra_snor_readl(info, TEGRA_SNOR_TIMING1_REG);
528 * writes to CAR and AHB can go out of order resulting in
529 * cases where AHB writes are flushed after the controller
530 * is in reset. A read before writing to CAR will force all
531 * pending writes on AHB to be flushed. The above snor register
532 * reads take care of this situation
535 tegra_periph_reset_assert(info->clk);
537 tegra_periph_reset_deassert(info->clk);
540 snor_config &= ~TEGRA_SNOR_CONFIG_GO;
541 dma_config &= ~TEGRA_SNOR_DMA_CFG_GO;
543 tegra_snor_writel(info, snor_config, TEGRA_SNOR_CONFIG_REG);
544 tegra_snor_writel(info, dma_config, TEGRA_SNOR_DMA_CFG_REG);
545 tegra_snor_writel(info, timing0, TEGRA_SNOR_TIMING0_REG);
546 tegra_snor_writel(info, timing1, TEGRA_SNOR_TIMING1_REG);
549 static int tegra_snor_controller_init(struct tegra_nor_info *info)
551 struct tegra_nor_chip_parms *chip_parm = &info->plat->chip_parms;
552 u32 width = info->plat->flash.width;
555 config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(0);
556 config |= TEGRA_SNOR_CONFIG_SNOR_CS(0);
557 config &= ~TEGRA_SNOR_CONFIG_DEVICE_TYPE; /* Select NOR */
558 config |= TEGRA_SNOR_CONFIG_WP; /* Enable writes */
561 config &= ~TEGRA_SNOR_CONFIG_WORDWIDE; /* 16 bit */
564 config |= TEGRA_SNOR_CONFIG_WORDWIDE; /* 32 bit */
569 switch (chip_parm->MuxMode) {
570 case NorMuxMode_ADNonMux:
571 config &= ~TEGRA_SNOR_CONFIG_MUX_MODE;
573 case NorMuxMode_ADMux:
574 config |= TEGRA_SNOR_CONFIG_MUX_MODE;
579 switch (chip_parm->ReadyActive) {
580 case NorReadyActive_WithData:
581 config &= ~TEGRA_SNOR_CONFIG_RDY_ACTIVE;
583 case NorReadyActive_BeforeData:
584 config |= TEGRA_SNOR_CONFIG_RDY_ACTIVE;
589 tegra_snor_writel(info, config, TEGRA_SNOR_CONFIG_REG);
590 info->init_config = config;
592 info->timing0_default = chip_parm->timing_default.timing0;
593 info->timing0_read = chip_parm->timing_read.timing0;
594 info->timing1_default = chip_parm->timing_default.timing1;
595 info->timing1_read = chip_parm->timing_read.timing1;
597 tegra_snor_writel(info, info->timing1_default, TEGRA_SNOR_TIMING1_REG);
598 tegra_snor_writel(info, info->timing0_default, TEGRA_SNOR_TIMING0_REG);
602 #if defined(CONFIG_TEGRA_EFS) || defined(CONFIG_TEGRA_PFLASH)
603 struct map_info *get_map_info(unsigned int bank_index)
605 struct map_info *map = &nor_gmi_map_list.map[bank_index];
608 int get_maps_no(void)
610 int no_of_maps = nor_gmi_map_list.n_maps;
613 unsigned long long getflashsize(void)
615 int flash_size = nor_gmi_map_list.totalflashsize;
620 static int flash_probe(struct tegra_nor_info *info)
622 struct mtd_info **mtd;
623 struct map_info *map_list = info->map;
624 struct map_info *map;
626 struct device *dev = info->dev;
627 int present_banks = 0;
628 unsigned long long size = 0;
629 struct tegra_nor_chip_parms *chip_parm = &info->plat->chip_parms;
632 mtd = devm_kzalloc(dev, sizeof(struct mtd_info *) *
633 info->n_maps, GFP_KERNEL);
636 dev_err(dev, "cannot allocate memory for mtd_info\n");
640 for (i = 0; i < info->n_maps; i++) {
642 mtd[i] = do_map_probe(info->plat->flash.map_name, map);
644 dev_err(dev, "cannot probe flash\n");
654 if (present_banks == 0) {
657 if (present_banks < info->n_maps)
658 info->concat_mtd = mtd_concat_create(info->mtd,
659 present_banks, DRV_NAME);
661 info->concat_mtd = mtd_concat_create(info->mtd,
662 info->n_maps, DRV_NAME);
664 if (!info->concat_mtd) {
665 dev_err(dev, "cannot concatenate flash\n");
669 info->concat_mtd->owner = THIS_MODULE;
671 #if defined(CONFIG_TEGRA_EFS) || defined(CONFIG_TEGRA_PFLASH)
672 nor_gmi_map_list.totalflashsize = size;
676 * Sometimes OE goes low after controller reset. Configuring
677 * as gpio to control correct behavior only for 32 bit Non Mux Mode
679 if ((chip_parm->MuxMode == NorMuxMode_ADNonMux) &&
680 (info->plat->flash.width == 4)) {
681 /* Configure OE as gpio and drive it low for read operations
682 and high otherwise */
683 if (gpio_request(info->plat->gmi_oe_n_gpio, "GMI_OE_N"))
685 gpio_direction_output(info->plat->gmi_oe_n_gpio, HIGH);
690 static int flash_maps_init(struct tegra_nor_info *info, struct resource *res)
692 struct map_info *map_list;
693 struct map_info *map;
694 struct flash_info *flinfo = &info->plat->info;
695 int num_chips = flinfo->num_chips;
697 struct tegra_nor_private *priv;
698 struct device *dev = info->dev;
700 priv = devm_kzalloc(dev, sizeof(struct tegra_nor_private) * num_chips,
703 dev_err(dev, "cannot allocate memory for" \
704 "private variable\n");
708 map_list = devm_kzalloc(dev, sizeof(struct map_info) * num_chips,
711 dev_err(dev, "cannot allocate memory for" \
716 for (i = 0; i < num_chips; i++) {
717 struct cs_info *csinfo = NULL;
718 struct gpio_state *state = NULL;
719 struct gpio_addr_info *adinfo = NULL;
720 struct gpio_addr *addr = NULL;
725 /* Setup private structure*/
727 priv[i].cs = flinfo->cs + i;
728 map->map_priv_1 = (unsigned long)&priv[i];
731 state = csinfo->gpio_cs;
732 adinfo = priv[i].info->adinfo;
734 /* Request Needed GPIO's */
736 for (j = 0; j < csinfo->num_cs_gpio; j++) {
737 /* For now ignore the request failures */
739 err = gpio_request(state[j].gpio_num, state[j].label);
740 gpio_direction_output(state[j].gpio_num, 0);
742 for (k = 0; k < adinfo->num_gpios; k++) {
744 err_add = gpio_request(addr[k].gpio_num, NULL);
745 gpio_direction_output(addr[k].gpio_num, 0);
748 map->name = dev_name(info->dev);
749 map->size = csinfo->size;
750 map->bankwidth = info->plat->flash.width;
751 map->virt = priv->cs->virt;
752 map->phys = priv->cs->phys;
754 simple_map_init(map);
755 map->read = tegra_nor_read;
756 map->write = tegra_nor_write;
757 map->copy_from = tegra_nor_copy_from;
758 map->device_node = info->dev->of_node;
761 info->n_maps = num_chips;
762 info->map = map_list;
766 static struct tegra_nor_platform_data *tegra_nor_parse_dt(
767 struct platform_device *pdev)
769 struct tegra_nor_platform_data *pdata = NULL;
770 struct device_node *np = pdev->dev.of_node;
771 struct device_node *np_cs_info;
772 struct cs_info nor_cs_info[8];
774 enum of_gpio_flags gpio_flags;
777 memset(nor_cs_info, 0, sizeof(nor_cs_info));
778 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
780 dev_err(&pdev->dev, "Memory alloc for pdata failed");
784 of_property_read_u32_array(np, "nvidia,timing-default",
785 (unsigned int *) &pdata->chip_parms.timing_default, 2);
786 of_property_read_u32_array(np, "nvidia,timing-read",
787 (unsigned int *) &pdata->chip_parms.timing_read, 2);
788 of_property_read_u32(np, "nvidia,nor-mux-mode",
789 (unsigned int *) &pdata->chip_parms.MuxMode);
790 of_property_read_u32(np, "nvidia,nor-read-mode",
791 (unsigned int *) &pdata->chip_parms.ReadMode);
792 of_property_read_u32(np, "nvidia,nor-page-length",
793 (unsigned int *) &pdata->chip_parms.PageLength);
794 of_property_read_u32(np, "nvidia,nor-ready-active",
795 (unsigned int *) &pdata->chip_parms.ReadyActive);
796 of_property_read_string(np, "nvidia,flash-map-name",
797 &pdata->flash.map_name);
798 of_property_read_u32(np, "nvidia,flash-width",
799 (unsigned int *) &pdata->flash.width);
800 of_property_read_u32(np, "nvidia,num-chips",
801 (unsigned int *) &pdata->info.num_chips);
802 pdata->gmi_oe_n_gpio = of_get_named_gpio(np, "nvidia,gmi-oe-pin", 0);
803 of_property_read_string(np, "nvidia,gmi_wait_pin",
804 &pdata->gmi_wait_pin);
806 for_each_child_of_node(np, np_cs_info) {
807 of_property_read_u32(np_cs_info, "nvidia,cs",
808 (unsigned int *) &nor_cs_info[i].cs);
809 of_property_read_u32(np_cs_info, "nvidia,num_cs_gpio",
810 (unsigned int *) &nor_cs_info[i].num_cs_gpio);
811 if (nor_cs_info[i].num_cs_gpio) {
813 int gpio_nums = nor_cs_info[i].num_cs_gpio;
815 nor_cs_info[i].gpio_cs = devm_kzalloc(&pdev->dev,
816 sizeof(*nor_cs_info[i].gpio_cs) * gpio_nums,
818 if (!nor_cs_info[i].gpio_cs) {
819 dev_err(&pdev->dev, "Memory alloc for" \
820 "nor_cs_info[%d].gpio_cs failed", i);
824 for (j = 0; j < nor_cs_info[i].num_cs_gpio; j++) {
825 nor_cs_info[i].gpio_cs[j].gpio_num =
826 of_get_named_gpio_flags(
831 nor_cs_info[i].gpio_cs[j].value = !(gpio_flags &
833 nor_cs_info[i].gpio_cs[j].label =
837 /*Read it as U64 always and then typecast to the required data type*/
838 of_property_read_u64(np_cs_info, "nvidia,phy_addr", &phy_addr);
839 nor_cs_info[i].phys = (resource_size_t)phy_addr;
840 nor_cs_info[i].virt = IO_ADDRESS(nor_cs_info[i].phys);
841 of_property_read_u32(np_cs_info, "nvidia,phy_size",
842 (unsigned int *) &nor_cs_info[i].size);
846 pdata->info.cs = devm_kzalloc(&pdev->dev,
847 sizeof(struct cs_info) * pdata->info.num_chips,
850 if (!pdata->info.cs) {
851 dev_err(&pdev->dev, "Memory alloc for pdata failed");
854 memcpy(pdata->info.cs, nor_cs_info,
855 sizeof(struct cs_info) * pdata->info.num_chips);
860 static int tegra_nor_probe(struct platform_device *pdev)
863 struct tegra_nor_platform_data *plat = pdev->dev.platform_data;
864 struct tegra_nor_info *info = NULL;
865 struct device *dev = &pdev->dev;
866 struct resource *res;
869 if (!plat && pdev->dev.of_node)
870 plat = tegra_nor_parse_dt(pdev);
873 pr_err("%s: no platform device info\n", __func__);
877 info = devm_kzalloc(dev, sizeof(struct tegra_nor_info),
882 /* Get NOR controller & map the same */
883 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
885 dev_err(dev, "no mem resource?\n");
889 info->base = devm_request_and_ioremap(dev, res);
891 dev_err(dev, "Couldn't ioremap regs\n");
898 #ifdef CONFIG_TEGRA_GMI_ACCESS_CONTROL
899 info->gmiLockHandle = register_gmi_device("tegra-nor", 0);
900 info->request_gmi_access = request_gmi_access;
901 info->release_gmi_access = release_gmi_access;
903 info->gmiLockHandle = 0;
904 info->request_gmi_access = NULL;
905 info->release_gmi_access = NULL;
907 /* Get address line gpios */
908 info->adinfo = &plat->addr;
910 /* Initialize mapping */
911 if (flash_maps_init(info, res)) {
912 dev_err(dev, "can't map NOR flash\n");
917 info->clk = devm_clk_get(dev, NULL);
918 if (IS_ERR(info->clk))
919 return PTR_ERR(info->clk);
921 err = clk_prepare_enable(info->clk);
925 /* Intialise the SNOR controller before probe */
926 err = tegra_snor_controller_init(info);
928 dev_err(dev, "Error initializing controller\n");
929 goto out_clk_disable;
932 init_completion(&info->dma_complete);
934 irq = platform_get_irq(pdev, 0);
936 dev_err(dev, "no irq resource?\n");
938 goto out_clk_disable;
941 /* Register SNOR DMA completion interrupt */
942 err = request_irq(irq, tegra_nor_isr, IRQF_DISABLED,
943 dev_name(dev), info);
945 dev_err(dev, "Failed to request irq %i\n", irq);
946 goto out_clk_disable;
948 info->dma_virt_buffer = dma_alloc_coherent(dev,
950 + MAX_DMA_BURST_SIZE,
951 &info->dma_phys_buffer,
953 if (!info->dma_virt_buffer) {
954 dev_err(&pdev->dev, "Could not allocate buffer for DMA");
956 goto out_clk_disable;
959 /* Probe the flash */
960 if (flash_probe(info)) {
961 dev_err(dev, "can't probe full flash\n");
963 goto out_dma_free_coherent;
967 plat->flash.parts = NULL;
969 platform_set_drvdata(pdev, info);
970 /* Parse partitions and register mtd */
971 err = mtd_device_parse_register(info->concat_mtd,
972 (const char **)part_probes, NULL, info->parts, 0);
975 goto out_dma_free_coherent;
977 #if defined(CONFIG_TEGRA_EFS) || defined(CONFIG_TEGRA_PFLASH)
978 nor_gmi_map_list.map = info->map;
979 nor_gmi_map_list.n_maps = info->n_maps;
984 out_dma_free_coherent:
985 dma_free_coherent(dev, TEGRA_SNOR_DMA_LIMIT + MAX_DMA_BURST_SIZE,
986 info->dma_virt_buffer, info->dma_phys_buffer);
988 clk_disable_unprepare(info->clk);
990 pr_err("Tegra NOR probe failed\n");
994 static int tegra_nor_remove(struct platform_device *pdev)
996 struct tegra_nor_info *info = platform_get_drvdata(pdev);
998 mtd_device_unregister(info->concat_mtd);
1000 dma_free_coherent(&pdev->dev, TEGRA_SNOR_DMA_LIMIT + MAX_DMA_BURST_SIZE,
1001 info->dma_virt_buffer, info->dma_phys_buffer);
1002 map_destroy(info->concat_mtd);
1003 clk_disable_unprepare(info->clk);
1008 static const struct of_device_id tegra_nor_dt_match[] = {
1009 { .compatible = "nvidia,tegra124-nor", .data = NULL },
1012 MODULE_DEVICE_TABLE(of, tegra_nor_dt_match);
1014 static struct platform_driver __refdata tegra_nor_driver = {
1015 .probe = tegra_nor_probe,
1016 .remove = tegra_nor_remove,
1019 .of_match_table = of_match_ptr(tegra_nor_dt_match),
1020 .owner = THIS_MODULE,
1025 module_platform_driver(tegra_nor_driver);
1027 MODULE_AUTHOR("Bharath H S <bhs@nvidia.com>");
1028 MODULE_DESCRIPTION("NOR Flash mapping driver for NVIDIA Tegra based boards");
1029 MODULE_LICENSE("GPL");
1030 MODULE_ALIAS("platform:" DRV_NAME);