| 1 |
/* |
| 2 |
* GRUB -- GRand Unified Bootloader |
| 3 |
* |
| 4 |
* Copyright (C) 2012 Google Inc. |
| 5 |
* Copyright (C) 2016 Free Software Foundation, Inc. |
| 6 |
* |
| 7 |
* This is based on depthcharge code. |
| 8 |
* |
| 9 |
* GRUB is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* GRUB is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <grub/mm.h> |
| 24 |
#include <grub/time.h> |
| 25 |
#include <grub/misc.h> |
| 26 |
#include <grub/fdtbus.h> |
| 27 |
#include <grub/machine/kernel.h> |
| 28 |
|
| 29 |
static grub_err_t |
| 30 |
spi_send (const struct grub_fdtbus_dev *dev, const void *data, grub_size_t sz) |
| 31 |
{ |
| 32 |
const grub_uint8_t *ptr = data, *end = ptr + sz; |
| 33 |
volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0); |
| 34 |
spi[2] = 0; |
| 35 |
spi[1] = sz - 1; |
| 36 |
spi[0] = ((1 << 18) | spi[0]) & ~(1 << 19); |
| 37 |
spi[2] = 1; |
| 38 |
while (ptr < end) |
| 39 |
{ |
| 40 |
while (spi[9] & 2); |
| 41 |
spi[256] = *ptr++; |
| 42 |
} |
| 43 |
while (spi[9] & 1); |
| 44 |
return GRUB_ERR_NONE; |
| 45 |
} |
| 46 |
|
| 47 |
static grub_err_t |
| 48 |
spi_receive (const struct grub_fdtbus_dev *dev, void *data, grub_size_t sz) |
| 49 |
{ |
| 50 |
grub_uint8_t *ptr = data, *end = ptr + sz; |
| 51 |
volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0); |
| 52 |
spi[2] = 0; |
| 53 |
spi[1] = sz - 1; |
| 54 |
spi[0] = ((1 << 19) | spi[0]) & ~(1 << 18); |
| 55 |
spi[2] = 1; |
| 56 |
while (ptr < end) |
| 57 |
{ |
| 58 |
while (spi[9] & 8); |
| 59 |
*ptr++ = spi[512]; |
| 60 |
} |
| 61 |
while (spi[9] & 1); |
| 62 |
return GRUB_ERR_NONE; |
| 63 |
} |
| 64 |
|
| 65 |
static grub_err_t |
| 66 |
spi_start (const struct grub_fdtbus_dev *dev) |
| 67 |
{ |
| 68 |
volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0); |
| 69 |
spi[3] = 1; |
| 70 |
return GRUB_ERR_NONE; |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
spi_stop (const struct grub_fdtbus_dev *dev) |
| 75 |
{ |
| 76 |
volatile grub_uint32_t *spi = grub_fdtbus_map_reg (dev, 0, 0); |
| 77 |
spi[3] = 0; |
| 78 |
} |
| 79 |
|
| 80 |
static grub_err_t |
| 81 |
spi_attach(const struct grub_fdtbus_dev *dev) |
| 82 |
{ |
| 83 |
if (!grub_fdtbus_is_mapping_valid (grub_fdtbus_map_reg (dev, 0, 0))) |
| 84 |
return GRUB_ERR_IO; |
| 85 |
|
| 86 |
return GRUB_ERR_NONE; |
| 87 |
} |
| 88 |
|
| 89 |
static struct grub_fdtbus_driver spi = |
| 90 |
{ |
| 91 |
.compatible = "rockchip,rk3288-spi", |
| 92 |
.attach = spi_attach, |
| 93 |
.send = spi_send, |
| 94 |
.receive = spi_receive, |
| 95 |
.start = spi_start, |
| 96 |
.stop = spi_stop, |
| 97 |
}; |
| 98 |
|
| 99 |
void |
| 100 |
grub_rk3288_spi_init (void) |
| 101 |
{ |
| 102 |
grub_fdtbus_register (&spi); |
| 103 |
} |