1dc35a10fSMarcin Wojtas /*
2dc35a10fSMarcin Wojtas * Driver for Marvell NETA network controller Buffer Manager.
3dc35a10fSMarcin Wojtas *
4dc35a10fSMarcin Wojtas * Copyright (C) 2015 Marvell
5dc35a10fSMarcin Wojtas *
6dc35a10fSMarcin Wojtas * Marcin Wojtas <mw@semihalf.com>
7dc35a10fSMarcin Wojtas *
8dc35a10fSMarcin Wojtas * This file is licensed under the terms of the GNU General Public
9dc35a10fSMarcin Wojtas * License version 2. This program is licensed "as is" without any
10dc35a10fSMarcin Wojtas * warranty of any kind, whether express or implied.
11dc35a10fSMarcin Wojtas */
12dc35a10fSMarcin Wojtas
13baa11ebcSGregory CLEMENT #include <linux/clk.h>
14dc35a10fSMarcin Wojtas #include <linux/genalloc.h>
15baa11ebcSGregory CLEMENT #include <linux/io.h>
16baa11ebcSGregory CLEMENT #include <linux/kernel.h>
17dc35a10fSMarcin Wojtas #include <linux/mbus.h>
18dc35a10fSMarcin Wojtas #include <linux/module.h>
19baa11ebcSGregory CLEMENT #include <linux/netdevice.h>
20dc35a10fSMarcin Wojtas #include <linux/of.h>
21965cbbecSGregory CLEMENT #include <linux/of_platform.h>
22baa11ebcSGregory CLEMENT #include <linux/platform_device.h>
23baa11ebcSGregory CLEMENT #include <linux/skbuff.h>
24baa11ebcSGregory CLEMENT #include <net/hwbm.h>
25dc35a10fSMarcin Wojtas #include "mvneta_bm.h"
26dc35a10fSMarcin Wojtas
27dc35a10fSMarcin Wojtas #define MVNETA_BM_DRIVER_NAME "mvneta_bm"
28dc35a10fSMarcin Wojtas #define MVNETA_BM_DRIVER_VERSION "1.0"
29dc35a10fSMarcin Wojtas
mvneta_bm_write(struct mvneta_bm * priv,u32 offset,u32 data)30dc35a10fSMarcin Wojtas static void mvneta_bm_write(struct mvneta_bm *priv, u32 offset, u32 data)
31dc35a10fSMarcin Wojtas {
32dc35a10fSMarcin Wojtas writel(data, priv->reg_base + offset);
33dc35a10fSMarcin Wojtas }
34dc35a10fSMarcin Wojtas
mvneta_bm_read(struct mvneta_bm * priv,u32 offset)35dc35a10fSMarcin Wojtas static u32 mvneta_bm_read(struct mvneta_bm *priv, u32 offset)
36dc35a10fSMarcin Wojtas {
37dc35a10fSMarcin Wojtas return readl(priv->reg_base + offset);
38dc35a10fSMarcin Wojtas }
39dc35a10fSMarcin Wojtas
mvneta_bm_pool_enable(struct mvneta_bm * priv,int pool_id)40dc35a10fSMarcin Wojtas static void mvneta_bm_pool_enable(struct mvneta_bm *priv, int pool_id)
41dc35a10fSMarcin Wojtas {
42dc35a10fSMarcin Wojtas u32 val;
43dc35a10fSMarcin Wojtas
44dc35a10fSMarcin Wojtas val = mvneta_bm_read(priv, MVNETA_BM_POOL_BASE_REG(pool_id));
45dc35a10fSMarcin Wojtas val |= MVNETA_BM_POOL_ENABLE_MASK;
46dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_POOL_BASE_REG(pool_id), val);
47dc35a10fSMarcin Wojtas
48dc35a10fSMarcin Wojtas /* Clear BM cause register */
49dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_INTR_CAUSE_REG, 0);
50dc35a10fSMarcin Wojtas }
51dc35a10fSMarcin Wojtas
mvneta_bm_pool_disable(struct mvneta_bm * priv,int pool_id)52dc35a10fSMarcin Wojtas static void mvneta_bm_pool_disable(struct mvneta_bm *priv, int pool_id)
53dc35a10fSMarcin Wojtas {
54dc35a10fSMarcin Wojtas u32 val;
55dc35a10fSMarcin Wojtas
56dc35a10fSMarcin Wojtas val = mvneta_bm_read(priv, MVNETA_BM_POOL_BASE_REG(pool_id));
57dc35a10fSMarcin Wojtas val &= ~MVNETA_BM_POOL_ENABLE_MASK;
58dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_POOL_BASE_REG(pool_id), val);
59dc35a10fSMarcin Wojtas }
60dc35a10fSMarcin Wojtas
mvneta_bm_config_set(struct mvneta_bm * priv,u32 mask)61dc35a10fSMarcin Wojtas static inline void mvneta_bm_config_set(struct mvneta_bm *priv, u32 mask)
62dc35a10fSMarcin Wojtas {
63dc35a10fSMarcin Wojtas u32 val;
64dc35a10fSMarcin Wojtas
65dc35a10fSMarcin Wojtas val = mvneta_bm_read(priv, MVNETA_BM_CONFIG_REG);
66dc35a10fSMarcin Wojtas val |= mask;
67dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_CONFIG_REG, val);
68dc35a10fSMarcin Wojtas }
69dc35a10fSMarcin Wojtas
mvneta_bm_config_clear(struct mvneta_bm * priv,u32 mask)70dc35a10fSMarcin Wojtas static inline void mvneta_bm_config_clear(struct mvneta_bm *priv, u32 mask)
71dc35a10fSMarcin Wojtas {
72dc35a10fSMarcin Wojtas u32 val;
73dc35a10fSMarcin Wojtas
74dc35a10fSMarcin Wojtas val = mvneta_bm_read(priv, MVNETA_BM_CONFIG_REG);
75dc35a10fSMarcin Wojtas val &= ~mask;
76dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_CONFIG_REG, val);
77dc35a10fSMarcin Wojtas }
78dc35a10fSMarcin Wojtas
mvneta_bm_pool_target_set(struct mvneta_bm * priv,int pool_id,u8 target_id,u8 attr)79dc35a10fSMarcin Wojtas static void mvneta_bm_pool_target_set(struct mvneta_bm *priv, int pool_id,
80dc35a10fSMarcin Wojtas u8 target_id, u8 attr)
81dc35a10fSMarcin Wojtas {
82dc35a10fSMarcin Wojtas u32 val;
83dc35a10fSMarcin Wojtas
84dc35a10fSMarcin Wojtas val = mvneta_bm_read(priv, MVNETA_BM_XBAR_POOL_REG(pool_id));
85dc35a10fSMarcin Wojtas val &= ~MVNETA_BM_TARGET_ID_MASK(pool_id);
86dc35a10fSMarcin Wojtas val &= ~MVNETA_BM_XBAR_ATTR_MASK(pool_id);
87dc35a10fSMarcin Wojtas val |= MVNETA_BM_TARGET_ID_VAL(pool_id, target_id);
88dc35a10fSMarcin Wojtas val |= MVNETA_BM_XBAR_ATTR_VAL(pool_id, attr);
89dc35a10fSMarcin Wojtas
90dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_XBAR_POOL_REG(pool_id), val);
91dc35a10fSMarcin Wojtas }
92dc35a10fSMarcin Wojtas
mvneta_bm_construct(struct hwbm_pool * hwbm_pool,void * buf)93baa11ebcSGregory CLEMENT int mvneta_bm_construct(struct hwbm_pool *hwbm_pool, void *buf)
94dc35a10fSMarcin Wojtas {
95baa11ebcSGregory CLEMENT struct mvneta_bm_pool *bm_pool =
96baa11ebcSGregory CLEMENT (struct mvneta_bm_pool *)hwbm_pool->priv;
97baa11ebcSGregory CLEMENT struct mvneta_bm *priv = bm_pool->priv;
98dc35a10fSMarcin Wojtas dma_addr_t phys_addr;
99dc35a10fSMarcin Wojtas
100dc35a10fSMarcin Wojtas /* In order to update buf_cookie field of RX descriptor properly,
101dc35a10fSMarcin Wojtas * BM hardware expects buf virtual address to be placed in the
102dc35a10fSMarcin Wojtas * first four bytes of mapped buffer.
103dc35a10fSMarcin Wojtas */
104dc35a10fSMarcin Wojtas *(u32 *)buf = (u32)buf;
105dc35a10fSMarcin Wojtas phys_addr = dma_map_single(&priv->pdev->dev, buf, bm_pool->buf_size,
106dc35a10fSMarcin Wojtas DMA_FROM_DEVICE);
107baa11ebcSGregory CLEMENT if (unlikely(dma_mapping_error(&priv->pdev->dev, phys_addr)))
108dc35a10fSMarcin Wojtas return -ENOMEM;
109dc35a10fSMarcin Wojtas
110baa11ebcSGregory CLEMENT mvneta_bm_pool_put_bp(priv, bm_pool, phys_addr);
111dc35a10fSMarcin Wojtas return 0;
112dc35a10fSMarcin Wojtas }
113baa11ebcSGregory CLEMENT EXPORT_SYMBOL_GPL(mvneta_bm_construct);
114dc35a10fSMarcin Wojtas
115dc35a10fSMarcin Wojtas /* Create pool */
mvneta_bm_pool_create(struct mvneta_bm * priv,struct mvneta_bm_pool * bm_pool)116dc35a10fSMarcin Wojtas static int mvneta_bm_pool_create(struct mvneta_bm *priv,
117dc35a10fSMarcin Wojtas struct mvneta_bm_pool *bm_pool)
118dc35a10fSMarcin Wojtas {
119dc35a10fSMarcin Wojtas struct platform_device *pdev = priv->pdev;
120dc35a10fSMarcin Wojtas u8 target_id, attr;
121dc35a10fSMarcin Wojtas int size_bytes, err;
122baa11ebcSGregory CLEMENT size_bytes = sizeof(u32) * bm_pool->hwbm_pool.size;
123dc35a10fSMarcin Wojtas bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, size_bytes,
124dc35a10fSMarcin Wojtas &bm_pool->phys_addr,
125dc35a10fSMarcin Wojtas GFP_KERNEL);
126dc35a10fSMarcin Wojtas if (!bm_pool->virt_addr)
127dc35a10fSMarcin Wojtas return -ENOMEM;
128dc35a10fSMarcin Wojtas
129dc35a10fSMarcin Wojtas if (!IS_ALIGNED((u32)bm_pool->virt_addr, MVNETA_BM_POOL_PTR_ALIGN)) {
130dc35a10fSMarcin Wojtas dma_free_coherent(&pdev->dev, size_bytes, bm_pool->virt_addr,
131dc35a10fSMarcin Wojtas bm_pool->phys_addr);
132dc35a10fSMarcin Wojtas dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
133dc35a10fSMarcin Wojtas bm_pool->id, MVNETA_BM_POOL_PTR_ALIGN);
134dc35a10fSMarcin Wojtas return -ENOMEM;
135dc35a10fSMarcin Wojtas }
136dc35a10fSMarcin Wojtas
137dc35a10fSMarcin Wojtas err = mvebu_mbus_get_dram_win_info(bm_pool->phys_addr, &target_id,
138dc35a10fSMarcin Wojtas &attr);
139dc35a10fSMarcin Wojtas if (err < 0) {
140dc35a10fSMarcin Wojtas dma_free_coherent(&pdev->dev, size_bytes, bm_pool->virt_addr,
141dc35a10fSMarcin Wojtas bm_pool->phys_addr);
142dc35a10fSMarcin Wojtas return err;
143dc35a10fSMarcin Wojtas }
144dc35a10fSMarcin Wojtas
145dc35a10fSMarcin Wojtas /* Set pool address */
146dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_POOL_BASE_REG(bm_pool->id),
147dc35a10fSMarcin Wojtas bm_pool->phys_addr);
148dc35a10fSMarcin Wojtas
149dc35a10fSMarcin Wojtas mvneta_bm_pool_target_set(priv, bm_pool->id, target_id, attr);
150dc35a10fSMarcin Wojtas mvneta_bm_pool_enable(priv, bm_pool->id);
151dc35a10fSMarcin Wojtas
152dc35a10fSMarcin Wojtas return 0;
153dc35a10fSMarcin Wojtas }
154dc35a10fSMarcin Wojtas
155dc35a10fSMarcin Wojtas /* Notify the driver that BM pool is being used as specific type and return the
156dc35a10fSMarcin Wojtas * pool pointer on success
157dc35a10fSMarcin Wojtas */
mvneta_bm_pool_use(struct mvneta_bm * priv,u8 pool_id,enum mvneta_bm_type type,u8 port_id,int pkt_size)158dc35a10fSMarcin Wojtas struct mvneta_bm_pool *mvneta_bm_pool_use(struct mvneta_bm *priv, u8 pool_id,
159dc35a10fSMarcin Wojtas enum mvneta_bm_type type, u8 port_id,
160dc35a10fSMarcin Wojtas int pkt_size)
161dc35a10fSMarcin Wojtas {
162dc35a10fSMarcin Wojtas struct mvneta_bm_pool *new_pool = &priv->bm_pools[pool_id];
163dc35a10fSMarcin Wojtas int num, err;
164dc35a10fSMarcin Wojtas
165dc35a10fSMarcin Wojtas if (new_pool->type == MVNETA_BM_LONG &&
166dc35a10fSMarcin Wojtas new_pool->port_map != 1 << port_id) {
167dc35a10fSMarcin Wojtas dev_err(&priv->pdev->dev,
168dc35a10fSMarcin Wojtas "long pool cannot be shared by the ports\n");
169dc35a10fSMarcin Wojtas return NULL;
170dc35a10fSMarcin Wojtas }
171dc35a10fSMarcin Wojtas
172dc35a10fSMarcin Wojtas if (new_pool->type == MVNETA_BM_SHORT && new_pool->type != type) {
173dc35a10fSMarcin Wojtas dev_err(&priv->pdev->dev,
174dc35a10fSMarcin Wojtas "mixing pools' types between the ports is forbidden\n");
175dc35a10fSMarcin Wojtas return NULL;
176dc35a10fSMarcin Wojtas }
177dc35a10fSMarcin Wojtas
178dc35a10fSMarcin Wojtas if (new_pool->pkt_size == 0 || type != MVNETA_BM_SHORT)
179dc35a10fSMarcin Wojtas new_pool->pkt_size = pkt_size;
180dc35a10fSMarcin Wojtas
181dc35a10fSMarcin Wojtas /* Allocate buffers in case BM pool hasn't been used yet */
182dc35a10fSMarcin Wojtas if (new_pool->type == MVNETA_BM_FREE) {
183baa11ebcSGregory CLEMENT struct hwbm_pool *hwbm_pool = &new_pool->hwbm_pool;
184baa11ebcSGregory CLEMENT
185baa11ebcSGregory CLEMENT new_pool->priv = priv;
186dc35a10fSMarcin Wojtas new_pool->type = type;
187dc35a10fSMarcin Wojtas new_pool->buf_size = MVNETA_RX_BUF_SIZE(new_pool->pkt_size);
188baa11ebcSGregory CLEMENT hwbm_pool->frag_size =
189dc35a10fSMarcin Wojtas SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(new_pool->pkt_size)) +
190dc35a10fSMarcin Wojtas SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
191baa11ebcSGregory CLEMENT hwbm_pool->construct = mvneta_bm_construct;
192baa11ebcSGregory CLEMENT hwbm_pool->priv = new_pool;
1936dcdd884SSebastian Andrzej Siewior mutex_init(&hwbm_pool->buf_lock);
194dc35a10fSMarcin Wojtas
195dc35a10fSMarcin Wojtas /* Create new pool */
196dc35a10fSMarcin Wojtas err = mvneta_bm_pool_create(priv, new_pool);
197dc35a10fSMarcin Wojtas if (err) {
198dc35a10fSMarcin Wojtas dev_err(&priv->pdev->dev, "fail to create pool %d\n",
199dc35a10fSMarcin Wojtas new_pool->id);
200dc35a10fSMarcin Wojtas return NULL;
201dc35a10fSMarcin Wojtas }
202dc35a10fSMarcin Wojtas
203dc35a10fSMarcin Wojtas /* Allocate buffers for this pool */
2046dcdd884SSebastian Andrzej Siewior num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
205baa11ebcSGregory CLEMENT if (num != hwbm_pool->size) {
206dc35a10fSMarcin Wojtas WARN(1, "pool %d: %d of %d allocated\n",
207baa11ebcSGregory CLEMENT new_pool->id, num, hwbm_pool->size);
208dc35a10fSMarcin Wojtas return NULL;
209dc35a10fSMarcin Wojtas }
210dc35a10fSMarcin Wojtas }
211dc35a10fSMarcin Wojtas
212dc35a10fSMarcin Wojtas return new_pool;
213dc35a10fSMarcin Wojtas }
214dc35a10fSMarcin Wojtas EXPORT_SYMBOL_GPL(mvneta_bm_pool_use);
215dc35a10fSMarcin Wojtas
216dc35a10fSMarcin Wojtas /* Free all buffers from the pool */
mvneta_bm_bufs_free(struct mvneta_bm * priv,struct mvneta_bm_pool * bm_pool,u8 port_map)217dc35a10fSMarcin Wojtas void mvneta_bm_bufs_free(struct mvneta_bm *priv, struct mvneta_bm_pool *bm_pool,
218dc35a10fSMarcin Wojtas u8 port_map)
219dc35a10fSMarcin Wojtas {
220dc35a10fSMarcin Wojtas int i;
221dc35a10fSMarcin Wojtas
222dc35a10fSMarcin Wojtas bm_pool->port_map &= ~port_map;
223dc35a10fSMarcin Wojtas if (bm_pool->port_map)
224dc35a10fSMarcin Wojtas return;
225dc35a10fSMarcin Wojtas
226dc35a10fSMarcin Wojtas mvneta_bm_config_set(priv, MVNETA_BM_EMPTY_LIMIT_MASK);
227dc35a10fSMarcin Wojtas
228baa11ebcSGregory CLEMENT for (i = 0; i < bm_pool->hwbm_pool.buf_num; i++) {
229dc35a10fSMarcin Wojtas dma_addr_t buf_phys_addr;
230dc35a10fSMarcin Wojtas u32 *vaddr;
231dc35a10fSMarcin Wojtas
232dc35a10fSMarcin Wojtas /* Get buffer physical address (indirect access) */
233dc35a10fSMarcin Wojtas buf_phys_addr = mvneta_bm_pool_get_bp(priv, bm_pool);
234dc35a10fSMarcin Wojtas
235dc35a10fSMarcin Wojtas /* Work-around to the problems when destroying the pool,
236dc35a10fSMarcin Wojtas * when it occurs that a read access to BPPI returns 0.
237dc35a10fSMarcin Wojtas */
238dc35a10fSMarcin Wojtas if (buf_phys_addr == 0)
239dc35a10fSMarcin Wojtas continue;
240dc35a10fSMarcin Wojtas
241dc35a10fSMarcin Wojtas vaddr = phys_to_virt(buf_phys_addr);
242dc35a10fSMarcin Wojtas if (!vaddr)
243dc35a10fSMarcin Wojtas break;
244dc35a10fSMarcin Wojtas
245dc35a10fSMarcin Wojtas dma_unmap_single(&priv->pdev->dev, buf_phys_addr,
246dc35a10fSMarcin Wojtas bm_pool->buf_size, DMA_FROM_DEVICE);
247baa11ebcSGregory CLEMENT hwbm_buf_free(&bm_pool->hwbm_pool, vaddr);
248dc35a10fSMarcin Wojtas }
249dc35a10fSMarcin Wojtas
250dc35a10fSMarcin Wojtas mvneta_bm_config_clear(priv, MVNETA_BM_EMPTY_LIMIT_MASK);
251dc35a10fSMarcin Wojtas
252dc35a10fSMarcin Wojtas /* Update BM driver with number of buffers removed from pool */
253baa11ebcSGregory CLEMENT bm_pool->hwbm_pool.buf_num -= i;
254dc35a10fSMarcin Wojtas }
255dc35a10fSMarcin Wojtas EXPORT_SYMBOL_GPL(mvneta_bm_bufs_free);
256dc35a10fSMarcin Wojtas
257dc35a10fSMarcin Wojtas /* Cleanup pool */
mvneta_bm_pool_destroy(struct mvneta_bm * priv,struct mvneta_bm_pool * bm_pool,u8 port_map)258dc35a10fSMarcin Wojtas void mvneta_bm_pool_destroy(struct mvneta_bm *priv,
259dc35a10fSMarcin Wojtas struct mvneta_bm_pool *bm_pool, u8 port_map)
260dc35a10fSMarcin Wojtas {
261baa11ebcSGregory CLEMENT struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
262dc35a10fSMarcin Wojtas bm_pool->port_map &= ~port_map;
263dc35a10fSMarcin Wojtas if (bm_pool->port_map)
264dc35a10fSMarcin Wojtas return;
265dc35a10fSMarcin Wojtas
266dc35a10fSMarcin Wojtas bm_pool->type = MVNETA_BM_FREE;
267dc35a10fSMarcin Wojtas
268dc35a10fSMarcin Wojtas mvneta_bm_bufs_free(priv, bm_pool, port_map);
269baa11ebcSGregory CLEMENT if (hwbm_pool->buf_num)
270dc35a10fSMarcin Wojtas WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
271dc35a10fSMarcin Wojtas
272dc35a10fSMarcin Wojtas if (bm_pool->virt_addr) {
273baa11ebcSGregory CLEMENT dma_free_coherent(&priv->pdev->dev,
274baa11ebcSGregory CLEMENT sizeof(u32) * hwbm_pool->size,
275dc35a10fSMarcin Wojtas bm_pool->virt_addr, bm_pool->phys_addr);
276dc35a10fSMarcin Wojtas bm_pool->virt_addr = NULL;
277dc35a10fSMarcin Wojtas }
278dc35a10fSMarcin Wojtas
279dc35a10fSMarcin Wojtas mvneta_bm_pool_disable(priv, bm_pool->id);
280dc35a10fSMarcin Wojtas }
281dc35a10fSMarcin Wojtas EXPORT_SYMBOL_GPL(mvneta_bm_pool_destroy);
282dc35a10fSMarcin Wojtas
mvneta_bm_pools_init(struct mvneta_bm * priv)283dc35a10fSMarcin Wojtas static void mvneta_bm_pools_init(struct mvneta_bm *priv)
284dc35a10fSMarcin Wojtas {
285dc35a10fSMarcin Wojtas struct device_node *dn = priv->pdev->dev.of_node;
286dc35a10fSMarcin Wojtas struct mvneta_bm_pool *bm_pool;
287dc35a10fSMarcin Wojtas char prop[15];
288dc35a10fSMarcin Wojtas u32 size;
289dc35a10fSMarcin Wojtas int i;
290dc35a10fSMarcin Wojtas
291dc35a10fSMarcin Wojtas /* Activate BM unit */
292dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_COMMAND_REG, MVNETA_BM_START_MASK);
293dc35a10fSMarcin Wojtas
294dc35a10fSMarcin Wojtas /* Create all pools with maximum size */
295dc35a10fSMarcin Wojtas for (i = 0; i < MVNETA_BM_POOLS_NUM; i++) {
296dc35a10fSMarcin Wojtas bm_pool = &priv->bm_pools[i];
297dc35a10fSMarcin Wojtas bm_pool->id = i;
298dc35a10fSMarcin Wojtas bm_pool->type = MVNETA_BM_FREE;
299dc35a10fSMarcin Wojtas
300dc35a10fSMarcin Wojtas /* Reset read pointer */
301dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_POOL_READ_PTR_REG(i), 0);
302dc35a10fSMarcin Wojtas
303dc35a10fSMarcin Wojtas /* Reset write pointer */
304dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_POOL_WRITE_PTR_REG(i), 0);
305dc35a10fSMarcin Wojtas
306dc35a10fSMarcin Wojtas /* Configure pool size according to DT or use default value */
307dc35a10fSMarcin Wojtas sprintf(prop, "pool%d,capacity", i);
308dc35a10fSMarcin Wojtas if (of_property_read_u32(dn, prop, &size)) {
309dc35a10fSMarcin Wojtas size = MVNETA_BM_POOL_CAP_DEF;
310dc35a10fSMarcin Wojtas } else if (size > MVNETA_BM_POOL_CAP_MAX) {
311dc35a10fSMarcin Wojtas dev_warn(&priv->pdev->dev,
312dc35a10fSMarcin Wojtas "Illegal pool %d capacity %d, set to %d\n",
313dc35a10fSMarcin Wojtas i, size, MVNETA_BM_POOL_CAP_MAX);
314dc35a10fSMarcin Wojtas size = MVNETA_BM_POOL_CAP_MAX;
315dc35a10fSMarcin Wojtas } else if (size < MVNETA_BM_POOL_CAP_MIN) {
316dc35a10fSMarcin Wojtas dev_warn(&priv->pdev->dev,
317dc35a10fSMarcin Wojtas "Illegal pool %d capacity %d, set to %d\n",
318dc35a10fSMarcin Wojtas i, size, MVNETA_BM_POOL_CAP_MIN);
319dc35a10fSMarcin Wojtas size = MVNETA_BM_POOL_CAP_MIN;
320dc35a10fSMarcin Wojtas } else if (!IS_ALIGNED(size, MVNETA_BM_POOL_CAP_ALIGN)) {
321dc35a10fSMarcin Wojtas dev_warn(&priv->pdev->dev,
322dc35a10fSMarcin Wojtas "Illegal pool %d capacity %d, round to %d\n",
323dc35a10fSMarcin Wojtas i, size, ALIGN(size,
324dc35a10fSMarcin Wojtas MVNETA_BM_POOL_CAP_ALIGN));
325dc35a10fSMarcin Wojtas size = ALIGN(size, MVNETA_BM_POOL_CAP_ALIGN);
326dc35a10fSMarcin Wojtas }
327baa11ebcSGregory CLEMENT bm_pool->hwbm_pool.size = size;
328dc35a10fSMarcin Wojtas
329dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_POOL_SIZE_REG(i),
330baa11ebcSGregory CLEMENT bm_pool->hwbm_pool.size);
331dc35a10fSMarcin Wojtas
332dc35a10fSMarcin Wojtas /* Obtain custom pkt_size from DT */
333dc35a10fSMarcin Wojtas sprintf(prop, "pool%d,pkt-size", i);
334dc35a10fSMarcin Wojtas if (of_property_read_u32(dn, prop, &bm_pool->pkt_size))
335dc35a10fSMarcin Wojtas bm_pool->pkt_size = 0;
336dc35a10fSMarcin Wojtas }
337dc35a10fSMarcin Wojtas }
338dc35a10fSMarcin Wojtas
mvneta_bm_default_set(struct mvneta_bm * priv)339dc35a10fSMarcin Wojtas static void mvneta_bm_default_set(struct mvneta_bm *priv)
340dc35a10fSMarcin Wojtas {
341dc35a10fSMarcin Wojtas u32 val;
342dc35a10fSMarcin Wojtas
343dc35a10fSMarcin Wojtas /* Mask BM all interrupts */
344dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_INTR_MASK_REG, 0);
345dc35a10fSMarcin Wojtas
346dc35a10fSMarcin Wojtas /* Clear BM cause register */
347dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_INTR_CAUSE_REG, 0);
348dc35a10fSMarcin Wojtas
349dc35a10fSMarcin Wojtas /* Set BM configuration register */
350dc35a10fSMarcin Wojtas val = mvneta_bm_read(priv, MVNETA_BM_CONFIG_REG);
351dc35a10fSMarcin Wojtas
352dc35a10fSMarcin Wojtas /* Reduce MaxInBurstSize from 32 BPs to 16 BPs */
353dc35a10fSMarcin Wojtas val &= ~MVNETA_BM_MAX_IN_BURST_SIZE_MASK;
354dc35a10fSMarcin Wojtas val |= MVNETA_BM_MAX_IN_BURST_SIZE_16BP;
355dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_CONFIG_REG, val);
356dc35a10fSMarcin Wojtas }
357dc35a10fSMarcin Wojtas
mvneta_bm_init(struct mvneta_bm * priv)358dc35a10fSMarcin Wojtas static int mvneta_bm_init(struct mvneta_bm *priv)
359dc35a10fSMarcin Wojtas {
360dc35a10fSMarcin Wojtas mvneta_bm_default_set(priv);
361dc35a10fSMarcin Wojtas
362dc35a10fSMarcin Wojtas /* Allocate and initialize BM pools structures */
363dc35a10fSMarcin Wojtas priv->bm_pools = devm_kcalloc(&priv->pdev->dev, MVNETA_BM_POOLS_NUM,
364dc35a10fSMarcin Wojtas sizeof(struct mvneta_bm_pool),
365dc35a10fSMarcin Wojtas GFP_KERNEL);
366dc35a10fSMarcin Wojtas if (!priv->bm_pools)
367dc35a10fSMarcin Wojtas return -ENOMEM;
368dc35a10fSMarcin Wojtas
369dc35a10fSMarcin Wojtas mvneta_bm_pools_init(priv);
370dc35a10fSMarcin Wojtas
371dc35a10fSMarcin Wojtas return 0;
372dc35a10fSMarcin Wojtas }
373dc35a10fSMarcin Wojtas
mvneta_bm_get_sram(struct device_node * dn,struct mvneta_bm * priv)374dc35a10fSMarcin Wojtas static int mvneta_bm_get_sram(struct device_node *dn,
375dc35a10fSMarcin Wojtas struct mvneta_bm *priv)
376dc35a10fSMarcin Wojtas {
377dc35a10fSMarcin Wojtas priv->bppi_pool = of_gen_pool_get(dn, "internal-mem", 0);
378dc35a10fSMarcin Wojtas if (!priv->bppi_pool)
379dc35a10fSMarcin Wojtas return -ENOMEM;
380dc35a10fSMarcin Wojtas
381dc35a10fSMarcin Wojtas priv->bppi_virt_addr = gen_pool_dma_alloc(priv->bppi_pool,
382dc35a10fSMarcin Wojtas MVNETA_BM_BPPI_SIZE,
383dc35a10fSMarcin Wojtas &priv->bppi_phys_addr);
384dc35a10fSMarcin Wojtas if (!priv->bppi_virt_addr)
385dc35a10fSMarcin Wojtas return -ENOMEM;
386dc35a10fSMarcin Wojtas
387dc35a10fSMarcin Wojtas return 0;
388dc35a10fSMarcin Wojtas }
389dc35a10fSMarcin Wojtas
mvneta_bm_put_sram(struct mvneta_bm * priv)390dc35a10fSMarcin Wojtas static void mvneta_bm_put_sram(struct mvneta_bm *priv)
391dc35a10fSMarcin Wojtas {
392dc35a10fSMarcin Wojtas gen_pool_free(priv->bppi_pool, priv->bppi_phys_addr,
393dc35a10fSMarcin Wojtas MVNETA_BM_BPPI_SIZE);
394dc35a10fSMarcin Wojtas }
395dc35a10fSMarcin Wojtas
mvneta_bm_get(struct device_node * node)396965cbbecSGregory CLEMENT struct mvneta_bm *mvneta_bm_get(struct device_node *node)
397965cbbecSGregory CLEMENT {
398965cbbecSGregory CLEMENT struct platform_device *pdev = of_find_device_by_node(node);
399965cbbecSGregory CLEMENT
400965cbbecSGregory CLEMENT return pdev ? platform_get_drvdata(pdev) : NULL;
401965cbbecSGregory CLEMENT }
402965cbbecSGregory CLEMENT EXPORT_SYMBOL_GPL(mvneta_bm_get);
403965cbbecSGregory CLEMENT
mvneta_bm_put(struct mvneta_bm * priv)404965cbbecSGregory CLEMENT void mvneta_bm_put(struct mvneta_bm *priv)
405965cbbecSGregory CLEMENT {
406965cbbecSGregory CLEMENT platform_device_put(priv->pdev);
407965cbbecSGregory CLEMENT }
408965cbbecSGregory CLEMENT EXPORT_SYMBOL_GPL(mvneta_bm_put);
409965cbbecSGregory CLEMENT
mvneta_bm_probe(struct platform_device * pdev)410dc35a10fSMarcin Wojtas static int mvneta_bm_probe(struct platform_device *pdev)
411dc35a10fSMarcin Wojtas {
412dc35a10fSMarcin Wojtas struct device_node *dn = pdev->dev.of_node;
413dc35a10fSMarcin Wojtas struct mvneta_bm *priv;
414dc35a10fSMarcin Wojtas int err;
415dc35a10fSMarcin Wojtas
416dc35a10fSMarcin Wojtas priv = devm_kzalloc(&pdev->dev, sizeof(struct mvneta_bm), GFP_KERNEL);
417dc35a10fSMarcin Wojtas if (!priv)
418dc35a10fSMarcin Wojtas return -ENOMEM;
419dc35a10fSMarcin Wojtas
420*37f76049SYueHaibing priv->reg_base = devm_platform_ioremap_resource(pdev, 0);
421dc35a10fSMarcin Wojtas if (IS_ERR(priv->reg_base))
422dc35a10fSMarcin Wojtas return PTR_ERR(priv->reg_base);
423dc35a10fSMarcin Wojtas
424dc35a10fSMarcin Wojtas priv->clk = devm_clk_get(&pdev->dev, NULL);
425dc35a10fSMarcin Wojtas if (IS_ERR(priv->clk))
426dc35a10fSMarcin Wojtas return PTR_ERR(priv->clk);
427dc35a10fSMarcin Wojtas err = clk_prepare_enable(priv->clk);
428dc35a10fSMarcin Wojtas if (err < 0)
429dc35a10fSMarcin Wojtas return err;
430dc35a10fSMarcin Wojtas
431dc35a10fSMarcin Wojtas err = mvneta_bm_get_sram(dn, priv);
432dc35a10fSMarcin Wojtas if (err < 0) {
433dc35a10fSMarcin Wojtas dev_err(&pdev->dev, "failed to allocate internal memory\n");
434dc35a10fSMarcin Wojtas goto err_clk;
435dc35a10fSMarcin Wojtas }
436dc35a10fSMarcin Wojtas
437dc35a10fSMarcin Wojtas priv->pdev = pdev;
438dc35a10fSMarcin Wojtas
439dc35a10fSMarcin Wojtas /* Initialize buffer manager internals */
440dc35a10fSMarcin Wojtas err = mvneta_bm_init(priv);
441dc35a10fSMarcin Wojtas if (err < 0) {
442dc35a10fSMarcin Wojtas dev_err(&pdev->dev, "failed to initialize controller\n");
443dc35a10fSMarcin Wojtas goto err_sram;
444dc35a10fSMarcin Wojtas }
445dc35a10fSMarcin Wojtas
446dc35a10fSMarcin Wojtas dn->data = priv;
447dc35a10fSMarcin Wojtas platform_set_drvdata(pdev, priv);
448dc35a10fSMarcin Wojtas
449dc35a10fSMarcin Wojtas dev_info(&pdev->dev, "Buffer Manager for network controller enabled\n");
450dc35a10fSMarcin Wojtas
451dc35a10fSMarcin Wojtas return 0;
452dc35a10fSMarcin Wojtas
453dc35a10fSMarcin Wojtas err_sram:
454dc35a10fSMarcin Wojtas mvneta_bm_put_sram(priv);
455dc35a10fSMarcin Wojtas err_clk:
456dc35a10fSMarcin Wojtas clk_disable_unprepare(priv->clk);
457dc35a10fSMarcin Wojtas return err;
458dc35a10fSMarcin Wojtas }
459dc35a10fSMarcin Wojtas
mvneta_bm_remove(struct platform_device * pdev)460dc35a10fSMarcin Wojtas static int mvneta_bm_remove(struct platform_device *pdev)
461dc35a10fSMarcin Wojtas {
462dc35a10fSMarcin Wojtas struct mvneta_bm *priv = platform_get_drvdata(pdev);
463dc35a10fSMarcin Wojtas u8 all_ports_map = 0xff;
464dc35a10fSMarcin Wojtas int i = 0;
465dc35a10fSMarcin Wojtas
466dc35a10fSMarcin Wojtas for (i = 0; i < MVNETA_BM_POOLS_NUM; i++) {
467dc35a10fSMarcin Wojtas struct mvneta_bm_pool *bm_pool = &priv->bm_pools[i];
468dc35a10fSMarcin Wojtas
469dc35a10fSMarcin Wojtas mvneta_bm_pool_destroy(priv, bm_pool, all_ports_map);
470dc35a10fSMarcin Wojtas }
471dc35a10fSMarcin Wojtas
472dc35a10fSMarcin Wojtas mvneta_bm_put_sram(priv);
473dc35a10fSMarcin Wojtas
474dc35a10fSMarcin Wojtas /* Dectivate BM unit */
475dc35a10fSMarcin Wojtas mvneta_bm_write(priv, MVNETA_BM_COMMAND_REG, MVNETA_BM_STOP_MASK);
476dc35a10fSMarcin Wojtas
477dc35a10fSMarcin Wojtas clk_disable_unprepare(priv->clk);
478dc35a10fSMarcin Wojtas
479dc35a10fSMarcin Wojtas return 0;
480dc35a10fSMarcin Wojtas }
481dc35a10fSMarcin Wojtas
482dc35a10fSMarcin Wojtas static const struct of_device_id mvneta_bm_match[] = {
483dc35a10fSMarcin Wojtas { .compatible = "marvell,armada-380-neta-bm" },
484dc35a10fSMarcin Wojtas { }
485dc35a10fSMarcin Wojtas };
486dc35a10fSMarcin Wojtas MODULE_DEVICE_TABLE(of, mvneta_bm_match);
487dc35a10fSMarcin Wojtas
488dc35a10fSMarcin Wojtas static struct platform_driver mvneta_bm_driver = {
489dc35a10fSMarcin Wojtas .probe = mvneta_bm_probe,
490dc35a10fSMarcin Wojtas .remove = mvneta_bm_remove,
491dc35a10fSMarcin Wojtas .driver = {
492dc35a10fSMarcin Wojtas .name = MVNETA_BM_DRIVER_NAME,
493dc35a10fSMarcin Wojtas .of_match_table = mvneta_bm_match,
494dc35a10fSMarcin Wojtas },
495dc35a10fSMarcin Wojtas };
496dc35a10fSMarcin Wojtas
497dc35a10fSMarcin Wojtas module_platform_driver(mvneta_bm_driver);
498dc35a10fSMarcin Wojtas
499dc35a10fSMarcin Wojtas MODULE_DESCRIPTION("Marvell NETA Buffer Manager Driver - www.marvell.com");
500dc35a10fSMarcin Wojtas MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
501dc35a10fSMarcin Wojtas MODULE_LICENSE("GPL v2");
502