1 /* 2 * Copyright (c) 2016, Linaro Limited 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <drivers/pl022_spi.h> 29 #include <drivers/pl061_gpio.h> 30 #include <hikey_peripherals.h> 31 #include <stdint.h> 32 #include <trace.h> 33 34 /* 35 * spi_init() must be run before calling this function. 36 * 37 * This runs a loopback test by default, so the SPI module will just 38 * receive what is transmitted, i.e. 0x01, 0x80, 0x00. 39 * 40 * In non-loopback mode, the transmitted value will elicit a readback of 41 * the measured value from the ADC chip on the Linksprite 96Boards 42 * Mezzanine card [1], which can be connected to either a sliding 43 * rheostat [2] or photoresistor [3]. 44 * 45 * [1] http://linksprite.com/wiki/index.php5?title=Linker_Mezzanine_card_for_96board 46 * [2] http://learn.linksprite.com/96-board/sliding-rheostat 47 * [3] http://learn.linksprite.com/96-board/photoresistor 48 */ 49 void spi_test(void) 50 { 51 struct pl061_data platform_pl061_data; 52 struct pl022_data platform_pl022_data; 53 vaddr_t gpio6_base = nsec_periph_base(GPIO6_BASE); 54 vaddr_t spi_base = nsec_periph_base(SPI_BASE); 55 uint8_t tx[3] = {0x01, 0x80, 0x00}; 56 uint8_t rx[3] = {0}; 57 size_t i, j, num_rxpkts, len = 3; 58 59 DMSG("gpio6_base: 0x%" PRIxVA "\n", gpio6_base); 60 DMSG("spi_base: 0x%" PRIxVA "\n", spi_base); 61 62 DMSG("configure GPIO\n"); 63 pl061_init(&platform_pl061_data); 64 pl061_register(gpio6_base, 6); 65 66 DMSG("enable software mode control for chip select\n"); 67 pl061_set_mode_control(GPIO6_2, PL061_MC_SW); 68 69 DMSG("mask/disable interrupt for chip select\n"); 70 platform_pl061_data.chip.ops->set_interrupt(GPIO6_2, 71 GPIO_INTERRUPT_DISABLE); 72 73 DMSG("configure SPI\n"); 74 platform_pl022_data.gpio = &platform_pl061_data.chip; 75 platform_pl022_data.base = spi_base; 76 platform_pl022_data.cs_gpio_base = gpio6_base; 77 platform_pl022_data.clk_hz = SPI_CLK_HZ; 78 platform_pl022_data.speed_hz = SPI_500_KHZ; 79 platform_pl022_data.cs_gpio_pin = GPIO6_2; 80 platform_pl022_data.mode = SPI_MODE0; 81 platform_pl022_data.data_size_bits = 8; 82 platform_pl022_data.loopback = true; 83 84 pl022_configure(&platform_pl022_data); 85 pl022_start(&platform_pl022_data); 86 87 for (j = 0; j < 20; j++) { 88 DMSG("SPI test loop: %zu\n", j); 89 platform_pl022_data.chip.ops->txrx8(&platform_pl022_data.chip, 90 tx, rx, len, &num_rxpkts); 91 for (i = 0; i < num_rxpkts; i++) 92 DMSG("rx[%zu] = 0x%x\n", i, rx[i]); 93 94 /* wait a bit */ 95 for (i = 0; i < 100000000; i++) 96 ; 97 } 98 99 pl022_end(&platform_pl022_data); 100 } 101