2 * Driver for MMC and SSD cards for Cavium OCTEON SOCs.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2012-2017 Cavium Inc.
10 #include <linux/dma-mapping.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/mmc/slot-gpio.h>
15 #include <linux/module.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
19 #include <asm/octeon/octeon.h>
22 #define CVMX_MIO_BOOT_CTL CVMX_ADD_IO_SEG(0x00011800000000D0ull)
25 * The l2c* functions below are used for the EMMC-17978 workaround.
27 * Due to a bug in the design of the MMC bus hardware, the 2nd to last
28 * cache block of a DMA read must be locked into the L2 Cache.
29 * Otherwise, data corruption may occur.
31 static inline void *phys_to_ptr(u64 address
)
33 return (void *)(address
| (1ull << 63)); /* XKPHYS */
37 * Lock a single line into L2. The line is zeroed before locking
38 * to make sure no dram accesses are made.
40 static void l2c_lock_line(u64 addr
)
42 char *addr_ptr
= phys_to_ptr(addr
);
45 "cache 31, %[line]" /* Unlock the line */
46 ::[line
] "m" (*addr_ptr
));
49 /* Unlock a single line in the L2 cache. */
50 static void l2c_unlock_line(u64 addr
)
52 char *addr_ptr
= phys_to_ptr(addr
);
55 "cache 23, %[line]" /* Unlock the line */
56 ::[line
] "m" (*addr_ptr
));
59 /* Locks a memory region in the L2 cache. */
60 static void l2c_lock_mem_region(u64 start
, u64 len
)
64 /* Round start/end to cache line boundaries */
65 end
= ALIGN(start
+ len
- 1, CVMX_CACHE_LINE_SIZE
);
66 start
= ALIGN(start
, CVMX_CACHE_LINE_SIZE
);
68 while (start
<= end
) {
70 start
+= CVMX_CACHE_LINE_SIZE
;
75 /* Unlock a memory region in the L2 cache. */
76 static void l2c_unlock_mem_region(u64 start
, u64 len
)
80 /* Round start/end to cache line boundaries */
81 end
= ALIGN(start
+ len
- 1, CVMX_CACHE_LINE_SIZE
);
82 start
= ALIGN(start
, CVMX_CACHE_LINE_SIZE
);
84 while (start
<= end
) {
85 l2c_unlock_line(start
);
86 start
+= CVMX_CACHE_LINE_SIZE
;
90 static void octeon_mmc_acquire_bus(struct cvm_mmc_host
*host
)
92 if (!host
->has_ciu3
) {
93 down(&octeon_bootbus_sem
);
94 /* For CN70XX, switch the MMC controller onto the bus. */
95 if (OCTEON_IS_MODEL(OCTEON_CN70XX
))
96 writeq(0, (void __iomem
*)CVMX_MIO_BOOT_CTL
);
98 down(&host
->mmc_serializer
);
102 static void octeon_mmc_release_bus(struct cvm_mmc_host
*host
)
105 up(&octeon_bootbus_sem
);
107 up(&host
->mmc_serializer
);
110 static void octeon_mmc_int_enable(struct cvm_mmc_host
*host
, u64 val
)
112 writeq(val
, host
->base
+ MIO_EMM_INT(host
));
114 writeq(val
, host
->base
+ MIO_EMM_INT_EN(host
));
117 static void octeon_mmc_set_shared_power(struct cvm_mmc_host
*host
, int dir
)
120 if (!atomic_dec_return(&host
->shared_power_users
))
121 gpiod_set_value_cansleep(host
->global_pwr_gpiod
, 0);
123 if (atomic_inc_return(&host
->shared_power_users
) == 1)
124 gpiod_set_value_cansleep(host
->global_pwr_gpiod
, 1);
127 static void octeon_mmc_dmar_fixup(struct cvm_mmc_host
*host
,
128 struct mmc_command
*cmd
,
129 struct mmc_data
*data
,
132 if (cmd
->opcode
!= MMC_WRITE_MULTIPLE_BLOCK
)
134 if (data
->blksz
* data
->blocks
<= 1024)
137 host
->n_minus_one
= addr
+ (data
->blksz
* data
->blocks
) - 1024;
138 l2c_lock_mem_region(host
->n_minus_one
, 512);
141 static void octeon_mmc_dmar_fixup_done(struct cvm_mmc_host
*host
)
143 if (!host
->n_minus_one
)
145 l2c_unlock_mem_region(host
->n_minus_one
, 512);
146 host
->n_minus_one
= 0;
149 static int octeon_mmc_probe(struct platform_device
*pdev
)
151 struct device_node
*cn
, *node
= pdev
->dev
.of_node
;
152 struct cvm_mmc_host
*host
;
158 host
= devm_kzalloc(&pdev
->dev
, sizeof(*host
), GFP_KERNEL
);
162 spin_lock_init(&host
->irq_handler_lock
);
163 sema_init(&host
->mmc_serializer
, 1);
165 host
->dev
= &pdev
->dev
;
166 host
->acquire_bus
= octeon_mmc_acquire_bus
;
167 host
->release_bus
= octeon_mmc_release_bus
;
168 host
->int_enable
= octeon_mmc_int_enable
;
169 host
->set_shared_power
= octeon_mmc_set_shared_power
;
170 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
) ||
171 OCTEON_IS_MODEL(OCTEON_CNF7XXX
)) {
172 host
->dmar_fixup
= octeon_mmc_dmar_fixup
;
173 host
->dmar_fixup_done
= octeon_mmc_dmar_fixup_done
;
176 host
->sys_freq
= octeon_get_io_clock_rate();
178 if (of_device_is_compatible(node
, "cavium,octeon-7890-mmc")) {
179 host
->big_dma_addr
= true;
180 host
->need_irq_handler_lock
= true;
181 host
->has_ciu3
= true;
184 * First seven are the EMM_INT bits 0..6, then two for
185 * the EMM_DMA_INT bits
187 for (i
= 0; i
< 9; i
++) {
188 mmc_irq
[i
] = platform_get_irq(pdev
, i
);
192 /* work around legacy u-boot device trees */
193 irq_set_irq_type(mmc_irq
[i
], IRQ_TYPE_EDGE_RISING
);
196 host
->big_dma_addr
= false;
197 host
->need_irq_handler_lock
= false;
198 host
->has_ciu3
= false;
199 /* First one is EMM second DMA */
200 for (i
= 0; i
< 2; i
++) {
201 mmc_irq
[i
] = platform_get_irq(pdev
, i
);
207 host
->last_slot
= -1;
209 base
= devm_platform_ioremap_resource(pdev
, 0);
211 return PTR_ERR(base
);
215 base
= devm_platform_ioremap_resource(pdev
, 1);
217 return PTR_ERR(base
);
218 host
->dma_base
= base
;
220 * To keep the register addresses shared we intentionally use
221 * a negative offset here, first register used on Octeon therefore
222 * starts at 0x20 (MIO_EMM_DMA_CFG).
224 host
->reg_off_dma
= -0x20;
226 ret
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(64));
231 * Clear out any pending interrupts that may be left over from
234 val
= readq(host
->base
+ MIO_EMM_INT(host
));
235 writeq(val
, host
->base
+ MIO_EMM_INT(host
));
237 if (host
->has_ciu3
) {
238 /* Only CMD_DONE, DMA_DONE, CMD_ERR, DMA_ERR */
239 for (i
= 1; i
<= 4; i
++) {
240 ret
= devm_request_irq(&pdev
->dev
, mmc_irq
[i
],
242 0, cvm_mmc_irq_names
[i
], host
);
244 dev_err(&pdev
->dev
, "Error: devm_request_irq %d\n",
250 ret
= devm_request_irq(&pdev
->dev
, mmc_irq
[0],
251 cvm_mmc_interrupt
, 0, KBUILD_MODNAME
,
254 dev_err(&pdev
->dev
, "Error: devm_request_irq %d\n",
260 host
->global_pwr_gpiod
= devm_gpiod_get_optional(&pdev
->dev
,
263 if (IS_ERR(host
->global_pwr_gpiod
)) {
264 dev_err(&pdev
->dev
, "Invalid power GPIO\n");
265 return PTR_ERR(host
->global_pwr_gpiod
);
268 platform_set_drvdata(pdev
, host
);
271 for_each_child_of_node(node
, cn
) {
273 of_platform_device_create(cn
, NULL
, &pdev
->dev
);
274 if (!host
->slot_pdev
[i
]) {
278 ret
= cvm_mmc_of_slot_probe(&host
->slot_pdev
[i
]->dev
, host
);
280 dev_err(&pdev
->dev
, "Error populating slots\n");
281 octeon_mmc_set_shared_power(host
, 0);
290 for (i
= 0; i
< CAVIUM_MAX_MMC
; i
++) {
292 cvm_mmc_of_slot_remove(host
->slot
[i
]);
293 if (host
->slot_pdev
[i
])
294 of_platform_device_destroy(&host
->slot_pdev
[i
]->dev
, NULL
);
299 static void octeon_mmc_remove(struct platform_device
*pdev
)
301 struct cvm_mmc_host
*host
= platform_get_drvdata(pdev
);
305 for (i
= 0; i
< CAVIUM_MAX_MMC
; i
++)
307 cvm_mmc_of_slot_remove(host
->slot
[i
]);
309 dma_cfg
= readq(host
->dma_base
+ MIO_EMM_DMA_CFG(host
));
310 dma_cfg
&= ~MIO_EMM_DMA_CFG_EN
;
311 writeq(dma_cfg
, host
->dma_base
+ MIO_EMM_DMA_CFG(host
));
313 octeon_mmc_set_shared_power(host
, 0);
316 static const struct of_device_id octeon_mmc_match
[] = {
318 .compatible
= "cavium,octeon-6130-mmc",
321 .compatible
= "cavium,octeon-7890-mmc",
325 MODULE_DEVICE_TABLE(of
, octeon_mmc_match
);
327 static struct platform_driver octeon_mmc_driver
= {
328 .probe
= octeon_mmc_probe
,
329 .remove
= octeon_mmc_remove
,
331 .name
= KBUILD_MODNAME
,
332 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
333 .of_match_table
= octeon_mmc_match
,
337 module_platform_driver(octeon_mmc_driver
);
339 MODULE_AUTHOR("Cavium Inc. <support@cavium.com>");
340 MODULE_DESCRIPTION("Low-level driver for Cavium OCTEON MMC/SSD card");
341 MODULE_LICENSE("GPL");