summaryrefslogtreecommitdiff
path: root/components/drivers/serial/device/serial-pl011.c
blob: 1f8cc3a26e34cb9faa5b854828431c62018e3b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-05     Bernard      The first version
 * 2022-08-24     GuEe-GUI     add OFW support
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/serial_dm.h>

#include <cpuport.h>

#include <ioremap.h>

#define PL011_OEIM          RT_BIT(10)  /* overrun error interrupt mask */
#define PL011_BEIM          RT_BIT(9)   /* break error interrupt mask */
#define PL011_PEIM          RT_BIT(8)   /* parity error interrupt mask */
#define PL011_FEIM          RT_BIT(7)   /* framing error interrupt mask */
#define PL011_RTIM          RT_BIT(6)   /* receive timeout interrupt mask */
#define PL011_TXIM          RT_BIT(5)   /* transmit interrupt mask */
#define PL011_RXIM          RT_BIT(4)   /* receive interrupt mask */
#define PL011_DSRMIM        RT_BIT(3)   /* DSR interrupt mask */
#define PL011_DCDMIM        RT_BIT(2)   /* DCD interrupt mask */
#define PL011_CTSMIM        RT_BIT(1)   /* CTS interrupt mask */
#define PL011_RIMIM         RT_BIT(0)   /* RI interrupt mask */

#define PL011_DR            0x000
#define PL011_FR            0x018
#define PL011_IBRD          0x024
#define PL011_FBRD          0x028
#define PL011_LCR           0x02c
#define PL011_CR            0x030
#define PL011_IMSC          0x038
#define PL011_RIS           0x03c
#define PL011_DMACR         0x048

#define PL011_LCRH_SPS      (1 << 7)
#define PL011_LCRH_WLEN_8   (3 << 5)
#define PL011_LCRH_WLEN_7   (2 << 5)
#define PL011_LCRH_WLEN_6   (1 << 5)
#define PL011_LCRH_WLEN_5   (0 << 5)
#define PL011_LCRH_FEN      (1 << 4)
#define PL011_LCRH_STP2     (1 << 3)
#define PL011_LCRH_EPS      (1 << 2)
#define PL011_LCRH_PEN      (1 << 1)
#define PL011_LCRH_BRK      (1 << 0)

#define PL011_LCRH_WLEN(n)  ((n - 5) << 5)

#define PL011_CR_CTSEN      RT_BIT(15)
#define PL011_CR_RTSEN      RT_BIT(14)
#define PL011_CR_RTS        RT_BIT(11)
#define PL011_CR_DTR        RT_BIT(10)
#define PL011_CR_RXE        RT_BIT(9)
#define PL011_CR_TXE        RT_BIT(8)
#define PL011_CR_LBE        RT_BIT(7)
#define PL011_CR_SIRLP      RT_BIT(2)
#define PL011_CR_SIREN      RT_BIT(1)
#define PL011_CR_UARTEN     RT_BIT(0)

struct pl011
{
    struct rt_serial_device parent;

    int irq;
    void *base;
    rt_ubase_t freq;
    struct rt_clk *clk;
    struct rt_clk *pclk;

    struct rt_spinlock spinlock;
};

#define raw_to_pl011(raw) rt_container_of(raw, struct pl011, parent)

rt_inline rt_uint32_t pl011_read(struct pl011 *pl011, int offset)
{
    return HWREG32(pl011->base + offset);
}

rt_inline void pl011_write(struct pl011 *pl011, int offset, rt_uint32_t value)
{
    HWREG32(pl011->base + offset) = value;
}

static void pl011_isr(int irqno, void *param)
{
    struct pl011 *pl011 = param;

    /* Check irq */
    if (pl011_read(pl011, PL011_RIS) & PL011_RXIM)
    {
        rt_base_t level = rt_spin_lock_irqsave(&pl011->spinlock);

        rt_hw_serial_isr(&pl011->parent, RT_SERIAL_EVENT_RX_IND);

        rt_spin_unlock_irqrestore(&pl011->spinlock, level);
    }
}

static rt_err_t pl011_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    rt_ubase_t quot;
    struct pl011 *pl011 = raw_to_pl011(serial);

    /* Clear UART setting */
    pl011_write(pl011, PL011_CR, 0);
    /* Disable FIFO */
    pl011_write(pl011, PL011_LCR, 0);

    if (cfg->baud_rate > pl011->freq / 16)
    {
        quot = RT_DIV_ROUND_CLOSEST(pl011->freq * 8, cfg->baud_rate);
    }
    else
    {
        quot = RT_DIV_ROUND_CLOSEST(pl011->freq * 4, cfg->baud_rate);
    }

    pl011_write(pl011, PL011_IBRD, quot >> 6);
    pl011_write(pl011, PL011_FBRD, quot & 0x3f);
    /* FIFO */
    pl011_write(pl011, PL011_LCR, PL011_LCRH_WLEN(cfg->data_bits));

    /* Art enable, TX/RX enable */
    pl011_write(pl011, PL011_CR, PL011_CR_UARTEN | PL011_CR_TXE | PL011_CR_RXE);

    return RT_EOK;
}

static rt_err_t pl011_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
    struct pl011 *pl011 = raw_to_pl011(serial);

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* Disable rx irq */
        pl011_write(pl011, PL011_IMSC, pl011_read(pl011, PL011_IMSC) & ~PL011_RXIM);

        rt_hw_interrupt_mask(pl011->irq);

        break;

    case RT_DEVICE_CTRL_SET_INT:
        /* Enable rx irq */
        pl011_write(pl011, PL011_IMSC, pl011_read(pl011, PL011_IMSC) | PL011_RXIM);

        rt_hw_interrupt_umask(pl011->irq);

        break;
    }

    return RT_EOK;
}

static int pl011_uart_putc(struct rt_serial_device *serial, char c)
{
    struct pl011 *pl011 = raw_to_pl011(serial);

    while (pl011_read(pl011, PL011_FR) & PL011_TXIM)
    {
        rt_hw_cpu_relax();
    }

    pl011_write(pl011, PL011_DR, c);

    return 1;
}

static int pl011_uart_getc(struct rt_serial_device *serial)
{
    int ch = -1;
    struct pl011 *pl011 = raw_to_pl011(serial);

    if (!(pl011_read(pl011, PL011_FR) & PL011_RXIM))
    {
        ch = pl011_read(pl011, PL011_DR);
    }

    return ch;
}

static const struct rt_uart_ops pl011_uart_ops =
{
    .configure = pl011_uart_configure,
    .control = pl011_uart_control,
    .putc = pl011_uart_putc,
    .getc = pl011_uart_getc,
};

static void pl011_early_kick(struct rt_fdt_earlycon *con, int why)
{
    struct pl011 *pl011 = raw_to_pl011(con->data);

    switch (why)
    {
    case FDT_EARLYCON_KICK_UPDATE:
        pl011->base = rt_ioremap((void *)con->mmio, con->size);
        break;

    case FDT_EARLYCON_KICK_COMPLETED:
        rt_iounmap(pl011->base);
        break;

    default:
        break;
    }
}

static rt_err_t pl011_early_setup(struct rt_fdt_earlycon *con, const char *options)
{
    rt_err_t err = RT_EOK;
    static struct pl011 pl011 = { };

    if (options && !con->mmio)
    {
        char *arg;

        con->mmio = RT_NULL;

        /*
         *  The pl011 serial port must already be setup and configured in early.
         *  Options are not yet supported.
         *  pl011,<addr>
         *  pl011,mmio32,<addr>
         */
        serial_for_each_args(arg, options)
        {
            if (!rt_strcmp(arg, "pl011") || !rt_strcmp(arg, "mmio32"))
            {
                continue;
            }
            if (!con->mmio)
            {
                con->mmio = (rt_ubase_t)serial_base_from_args(arg);
                break;
            }
        }
    }

    if (!con->size)
    {
        con->size = 0x1000;
    }

    if (con->mmio)
    {
        pl011.base = rt_ioremap_early((void *)con->mmio, con->size);
    }

    if (pl011.base)
    {
        con->console_putc = (typeof(con->console_putc))&pl011_uart_putc;
        con->console_kick = pl011_early_kick;
        con->data = &pl011.parent;
        pl011.parent.config = (typeof(pl011.parent.config))RT_SERIAL_CONFIG_DEFAULT;
    }
    else
    {
        err = -RT_ERROR;
    }

    return err;
}
RT_FDT_EARLYCON_EXPORT(pl011, "pl011", "arm,pl011", pl011_early_setup);

static void pl011_free(struct pl011 *pl011)
{
    if (pl011->base)
    {
        rt_iounmap(pl011->base);
    }

    if (!rt_is_err_or_null(pl011->clk))
    {
        rt_clk_disable(pl011->clk);
        rt_clk_put(pl011->clk);
    }

    if (!rt_is_err_or_null(pl011->pclk))
    {
        rt_clk_disable_unprepare(pl011->pclk);
        rt_clk_put(pl011->pclk);
    }

    rt_free(pl011);
}

static rt_err_t pl011_probe(struct rt_platform_device *pdev)
{
    rt_err_t err;
    const char *name;
    char isr_name[RT_NAME_MAX];
    struct rt_device *dev = &pdev->parent;
    struct pl011 *pl011 = rt_calloc(1, sizeof(*pl011));
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

    if (!pl011)
    {
        return -RT_ENOMEM;
    }

    pl011->base = rt_dm_dev_iomap(dev, 0);

    if (!pl011->base)
    {
        err = -RT_EIO;

        goto _fail;
    }

    pl011->irq = rt_dm_dev_get_irq(dev, 0);

    if (pl011->irq < 0)
    {
        err = pl011->irq;

        goto _fail;
    }

    pl011->clk = rt_clk_get_by_index(dev, 0);

    if (rt_is_err(pl011->clk))
    {
        err = rt_ptr_err(pl011->clk);

        goto _fail;
    }

    pl011->pclk = rt_clk_get_by_name(dev, "apb_pclk");

    if (rt_is_err(pl011->pclk))
    {
        err = rt_ptr_err(pl011->pclk);

        goto _fail;
    }

    if ((err = rt_clk_prepare_enable(pl011->pclk)))
    {
        goto _fail;
    }

    rt_dm_dev_bind_fwdata(&pl011->parent.parent, dev->ofw_node, &pl011->parent);

    rt_clk_enable(pl011->clk);
    pl011->freq = rt_clk_get_rate(pl011->clk);

    dev->user_data = pl011;

    pl011->parent.ops = &pl011_uart_ops;
    pl011->parent.config = config;

    rt_spin_lock_init(&pl011->spinlock);

    serial_dev_set_name(&pl011->parent);
    name = rt_dm_dev_get_name(&pl011->parent.parent);

    rt_hw_serial_register(&pl011->parent, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, pl011);
    rt_snprintf(isr_name, RT_NAME_MAX, "%s-pl011", name);
    rt_hw_interrupt_install(pl011->irq, pl011_isr, pl011, isr_name);

    return RT_EOK;

_fail:
    pl011_free(pl011);

    return err;
}

static rt_err_t pl011_remove(struct rt_platform_device *pdev)
{
    struct rt_device *dev = &pdev->parent;
    struct pl011 *pl011 = dev->user_data;

    rt_dm_dev_unbind_fwdata(dev, RT_NULL);

    rt_hw_interrupt_mask(pl011->irq);
    rt_pic_detach_irq(pl011->irq, pl011);

    rt_device_unregister(&pl011->parent.parent);

    pl011_free(pl011);

    return RT_EOK;
}

static const struct rt_ofw_node_id pl011_ofw_ids[] =
{
    { .type = "ttyAMA", .compatible = "arm,pl011" },
    { .type = "ttyAMA", .compatible = "arm,pl011-axi" },
    { /* sentinel */ }
};

static struct rt_platform_driver pl011_driver =
{
    .name = "serial-pl011",
    .ids = pl011_ofw_ids,

    .probe = pl011_probe,
    .remove = pl011_remove,
};

static int pl011_drv_register(void)
{
    rt_platform_driver_register(&pl011_driver);

    return 0;
}
INIT_PLATFORM_EXPORT(pl011_drv_register);